<?php

namespace Tests\Feature\Audit;

use App\Models\Central\Company;
use App\Models\Central\Plan;
use App\Models\Tenant\Client;
use App\Models\Tenant\Vehicle;
use App\Models\Tenant\WorkOrder;
use App\Models\Tenant\WorkOrderPart;
use App\Models\Tenant\WorkOrderWork;
use App\Services\WorkOrderPdfService;
use App\Tenancy\TenantManager;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class WorkOrderPdfServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_pdf_generates_with_works_and_parts(): void
    {
        $ctx = $this->bootTenant();
        $client = Client::create(['name' => 'PdfClient', 'phone' => '+37300100200', 'type' => 'individual', 'status' => 'active']);
        $vehicle = Vehicle::create([
            'client_id' => $client->id, 'make' => 'Audi', 'model' => 'A4', 'plate' => 'PDF-001',
        ]);
        $wo = WorkOrder::create([
            'number' => WorkOrder::generateNumber($ctx['company']->id),
            'client_id' => $client->id, 'vehicle_id' => $vehicle->id,
            'opened_at' => today(), 'status' => 'in_work',
            'complaint' => 'Zgomot la frânare',
        ]);
        WorkOrderWork::create([
            'work_order_id' => $wo->id, 'name' => 'Schimb plăcuțe',
            'hours' => 1.5, 'price_per_hour' => 400, 'status' => 'done',
        ]);
        WorkOrderPart::create([
            'work_order_id' => $wo->id, 'name' => 'Plăcuțe TRW',
            'qty' => 1, 'buy_price' => 200, 'sell_price' => 350,
            'status' => 'installed',
        ]);

        $svc = app(WorkOrderPdfService::class);
        $pdf = $svc->generate($wo);
        $output = $pdf->output();

        $this->assertStringStartsWith('%PDF', $output, 'binary starts with PDF magic');
        $this->assertGreaterThan(2000, strlen($output), 'non-trivial PDF size');

        $filename = $svc->filename($wo);
        $this->assertStringContainsString($wo->number, $filename);
        $this->assertStringEndsWith('.pdf', $filename);
    }

    public function test_pdf_handles_wo_with_no_lines(): void
    {
        $ctx = $this->bootTenant();
        $client = Client::create(['name' => 'X', 'phone' => '+37399000000', 'type' => 'individual', 'status' => 'active']);
        $vehicle = Vehicle::create(['client_id' => $client->id, 'make' => 'X', 'model' => 'Y', 'plate' => 'EMPTY-1']);
        $wo = WorkOrder::create([
            'number' => WorkOrder::generateNumber($ctx['company']->id),
            'client_id' => $client->id, 'vehicle_id' => $vehicle->id,
            'opened_at' => today(), 'status' => 'new',
        ]);

        $pdf = app(WorkOrderPdfService::class)->generate($wo);
        $this->assertStringStartsWith('%PDF', $pdf->output());
    }

    private function bootTenant(): array
    {
        $plan = Plan::firstOrCreate(['slug' => 'test'], ['name' => 'T', 'price' => 0, 'features' => []]);
        $company = Company::create([
            'plan_id' => $plan->id, 'slug' => 'pdf-' . uniqid(),
            'name' => 'PDF Co', 'status' => 'active',
        ]);
        app(TenantManager::class)->setCurrent($company);
        return compact('company');
    }
}
