<?php

namespace Tests\Feature;

use App\Models\Sale;
use App\Models\User;
use App\Services\InvoicePdfService;
use App\Services\ReportingService;
use Database\Seeders\DatabaseSeeder;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class ReportingAndPdfTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->seed(DatabaseSeeder::class);
        $this->actingAs(User::where('username', 'admin')->firstOrFail());
    }

    public function test_every_report_in_the_catalog_executes_against_posted_data(): void
    {
        $service = app(ReportingService::class);
        foreach ($service->catalog() as $reports) {
            foreach (array_keys($reports) as $type) {
                $report = $service->run($type, '2026-01-01', '2026-12-31');
                $this->assertArrayHasKey('columns', $report, $type);
                $this->assertArrayHasKey('rows', $report, $type);
            }
        }
    }

    public function test_finalized_invoice_generates_a_real_pdf(): void
    {
        $sale = Sale::where('status', 'finalized')->firstOrFail();
        $response = app(InvoicePdfService::class)->sale($sale);

        $this->assertSame('application/pdf', $response->headers->get('Content-Type'));
        $this->assertStringStartsWith('%PDF', $response->getContent());
        $this->assertGreaterThan(20_000, strlen($response->getContent()));
    }
}
