<?php

declare(strict_types=1);

namespace Tests\Feature;

use App\Models\Customer;
use App\Models\Estimate;
use App\Models\User;
use App\Models\Vehicle;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class EstimatesTest extends TestCase
{
    use RefreshDatabase;

    public function test_estimate_model_relationships_work(): void
    {
        // Create test data
        $customer = Customer::factory()->create();
        $vehicle = Vehicle::factory()->create(['customer_id' => $customer->id]);
        $user = User::factory()->create();

        // Create standalone estimate
        $estimate = Estimate::factory()->create([
            'customer_id' => $customer->id,
            'vehicle_id' => $vehicle->id,
            'prepared_by_id' => $user->id,
            'job_card_id' => null,
            'diagnosis_id' => null,
        ]);

        // Test relationships
        $this->assertEquals($customer->id, $estimate->customer->id);
        $this->assertEquals($vehicle->id, $estimate->vehicle->id);
        $this->assertEquals($user->id, $estimate->preparedBy->id);
        $this->assertNull($estimate->job_card_id);
        $this->assertNull($estimate->diagnosis_id);
    }

    public function test_estimate_customer_and_vehicle_accessors(): void
    {
        $customer = Customer::factory()->create();
        $vehicle = Vehicle::factory()->create(['customer_id' => $customer->id]);
        $user = User::factory()->create();

        // Create standalone estimate
        $estimate = Estimate::factory()->create([
            'customer_id' => $customer->id,
            'vehicle_id' => $vehicle->id,
            'prepared_by_id' => $user->id,
            'job_card_id' => null,
            'diagnosis_id' => null,
        ]);

        // Test customer accessor (should return direct customer)
        $this->assertEquals($customer->name, $estimate->estimate_customer->name);

        // Test vehicle accessor (should return direct vehicle)
        $this->assertEquals($vehicle->make, $estimate->estimate_vehicle->make);
    }

    public function test_estimate_stats_calculation_with_different_statuses(): void
    {
        $customer = Customer::factory()->create();
        $vehicle = Vehicle::factory()->create(['customer_id' => $customer->id]);
        $user = User::factory()->create();

        // Create estimates with different statuses
        Estimate::factory()->create([
            'customer_id' => $customer->id,
            'vehicle_id' => $vehicle->id,
            'prepared_by_id' => $user->id,
            'job_card_id' => null,
            'diagnosis_id' => null,
            'status' => 'draft',
            'total_amount' => 100.00,
        ]);

        Estimate::factory()->create([
            'customer_id' => $customer->id,
            'vehicle_id' => $vehicle->id,
            'prepared_by_id' => $user->id,
            'job_card_id' => null,
            'diagnosis_id' => null,
            'status' => 'sent',
            'total_amount' => 200.00,
        ]);

        Estimate::factory()->create([
            'customer_id' => $customer->id,
            'vehicle_id' => $vehicle->id,
            'prepared_by_id' => $user->id,
            'job_card_id' => null,
            'diagnosis_id' => null,
            'status' => 'approved',
            'total_amount' => 300.00,
        ]);

        // Verify the estimates were created with correct status and amounts
        $this->assertEquals(1, Estimate::where('status', 'draft')->count());
        $this->assertEquals(1, Estimate::where('status', 'sent')->count());
        $this->assertEquals(1, Estimate::where('status', 'approved')->count());
        $this->assertEquals(600.00, Estimate::sum('total_amount'));
    }
}
