<?php

namespace App\Http\Controllers;

use App\Models\CompanyProfile;
use App\Models\VatSetting;
use App\Services\MoneyService;
use App\Services\ZatcaPhase1QrService;
use Illuminate\Http\Request;

class InvoicePreviewController extends Controller
{
    public function showSample()
    {
        $company = CompanyProfile::getProfile();
        $vatSetting = VatSetting::getSettings();

        $invoiceData = [
            'invoice_no' => 'TI-000001',
            'invoice_date' => now()->format('Y-m-d'),
            'invoice_time' => now()->format('H:i:s'),
            'timestamp' => now()->toIso8601String(),
            'po_no' => 'PO-88992',
            'po_date' => now()->subDay()->format('Y-m-d'),
            'customer' => [
                'name_en' => 'SAEEDA MOHAMMED AHMED AL MASOUDI GROCERY',
                'name_ar' => 'بقالة سعيدة محمد أحمد المسعودي',
                'vat_no' => '310987654300003',
                'customer_id' => 'CUST-00912',
                'cr_no' => '1010998877',
                'building_no' => '4321',
                'street_en' => 'Al Madinah Road',
                'street_ar' => 'طريق المدينة المنورة',
                'district_en' => 'Al Rawdah',
                'district_ar' => 'الروضة',
                'city_en' => 'Jeddah',
                'city_ar' => 'جدة',
                'postal_code' => '21432',
                'additional_no' => '8765',
                'country' => 'Saudi Arabia',
                'contact_no' => '+966 54 111 2233',
            ],
            'items' => [
                [
                    'si' => 1,
                    'desc_en' => 'TOMATO PASTE SAUDI 135 GM',
                    'desc_ar' => 'معجون طماطم سعودي 135 جرام',
                    'unit' => 'CTN',
                    'qty' => 5.00,
                    'unit_price' => 65.22,
                    'gross' => 326.10,
                    'vat_rate' => 15.00,
                    'vat_amount' => 48.91,
                    'total' => 375.01,
                ],
                [
                    'si' => 2,
                    'desc_en' => 'MILK POWDER SAUDI 2.5 KG',
                    'desc_ar' => 'حليب مجفف سعودي 2.5 كجم',
                    'unit' => 'CAN',
                    'qty' => 4.00,
                    'unit_price' => 78.26,
                    'gross' => 313.04,
                    'vat_rate' => 15.00,
                    'vat_amount' => 46.96,
                    'total' => 360.00,
                ],
                [
                    'si' => 3,
                    'desc_en' => 'RICE BASMATI 10 KG',
                    'desc_ar' => 'أرز بسمتي 10 كجم',
                    'unit' => 'BAG',
                    'qty' => 3.00,
                    'unit_price' => 113.04,
                    'gross' => 339.12,
                    'vat_rate' => 15.00,
                    'vat_amount' => 50.87,
                    'total' => 390.00,
                ],
                [
                    'si' => 4,
                    'desc_en' => 'SUNFLOWER OIL 1.5 L',
                    'desc_ar' => 'زيت عباد الشمس 1.5 لتر',
                    'unit' => 'BOT',
                    'qty' => 10.00,
                    'unit_price' => 18.26,
                    'gross' => 182.60,
                    'vat_rate' => 15.00,
                    'vat_amount' => 27.39,
                    'total' => 209.99,
                ],
            ],
            'total_gross' => 1160.86,
            'discount' => 0.00,
            'total_vat' => 174.13,
            'grand_total' => 1334.99,
        ];

        // Format Grand Total to match exact sample rounding: 1335.00
        $grandTotalFormatted = number_format(1335.00, 2, '.', '');
        $vatTotalFormatted = number_format(174.13, 2, '.', '');

        // Generate ZATCA Phase 1 TLV Base64 & SVG QR Code
        $tlvBase64 = ZatcaPhase1QrService::generateTlvBase64(
            $company->company_name_en,
            $company->vat_number ?? '302176698800003',
            $invoiceData['timestamp'],
            $grandTotalFormatted,
            $vatTotalFormatted
        );

        $qrSvg = ZatcaPhase1QrService::generateQrSvg($tlvBase64, 140);

        $amountInWordsEn = MoneyService::amountToWordsEn(1335.00, 'Saudi Riyal');
        $amountInWordsAr = MoneyService::amountToWordsAr(1335.00, 'ريال سعودي');

        return view('invoices.tax_invoice_a4', compact(
            'company',
            'vatSetting',
            'invoiceData',
            'qrSvg',
            'amountInWordsEn',
            'amountInWordsAr'
        ));
    }
}
