<?php

declare(strict_types=1);

namespace Tests\Feature\Estimates;

use App\Livewire\Estimates\Show;
use App\Models\Estimate;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Livewire\Livewire;
use Tests\TestCase;

class EstimatePdfTest extends TestCase
{
    use RefreshDatabase;

    public function test_estimate_show_component_can_be_mounted(): void
    {
        // Create test user with admin permissions
        $user = User::factory()->create();
        $user->assignRole('admin');

        // Create a simple estimate
        $estimate = Estimate::factory()->create([
            'estimate_number' => 'TEST-001',
            'status' => 'draft',
            'total_amount' => 100.00,
        ]);

        // Test the Show component can be mounted
        $component = Livewire::actingAs($user)
            ->test(Show::class, ['estimate' => $estimate])
            ->assertOk();

        // Verify estimate is loaded correctly
        $this->assertEquals($estimate->id, $component->estimate->id);
        $this->assertEquals($estimate->estimate_number, $component->estimate->estimate_number);
    }

    public function test_pdf_template_renders_without_errors(): void
    {
        $estimate = Estimate::factory()->create([
            'estimate_number' => 'TEST-PDF-001',
            'status' => 'draft',
            'total_amount' => 150.00,
        ]);

        // Test that the PDF view can be rendered
        $view = view('estimates.pdf', compact('estimate'));

        $this->assertNotNull($view);

        $html = $view->render();
        $this->assertStringContainsString('ESTIMATE', $html);
        $this->assertStringContainsString($estimate->estimate_number, $html);
    }
}
