<?php

namespace Tests\Feature;

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

class InternalMarginRecomputeTest extends TestCase
{
    use RefreshDatabase;

    private Company $company;
    private User $mechanic;
    private WorkOrder $wo;

    protected function setUp(): void
    {
        parent::setUp();
        $plan = Plan::firstOrCreate(['slug' => 'test'], ['name' => 'T', 'price' => 0, 'features' => []]);
        $this->company = Company::create(['plan_id' => $plan->id, 'slug' => 'rec-' . uniqid(), 'name' => 'REC', 'status' => 'active']);
        app(TenantManager::class)->setCurrent($this->company);

        $this->mechanic = User::create(['name' => 'Andrei', 'email' => 'a@e.com', 'password' => bcrypt('x'), 'role' => 'mechanic', 'status' => 'active', 'internal_margin_pct' => 20]);
        $client = Client::create(['name' => 'C', 'phone' => '+37399000000', 'type' => 'individual', 'status' => 'active']);
        $vehicle = Vehicle::create(['client_id' => $client->id, 'make' => 'BMW', 'model' => 'X5', 'plate' => 'RC-1']);
        $this->wo = WorkOrder::create([
            'number' => WorkOrder::generateNumber($this->company->id),
            'client_id' => $client->id, 'vehicle_id' => $vehicle->id, 'master_id' => $this->mechanic->id,
            'opened_at' => today(), 'status' => 'in_work', 'total' => 0,
        ]);
    }

    public function test_toggling_wo_apply_margin_off_recomputes_all_existing_lines(): void
    {
        // Create 2 lines with margin on (default)
        $w1 = WorkOrderWork::create(['work_order_id' => $this->wo->id, 'master_id' => $this->mechanic->id, 'name' => 'A', 'hours' => 1, 'price_per_hour' => 250]);
        $w2 = WorkOrderWork::create(['work_order_id' => $this->wo->id, 'master_id' => $this->mechanic->id, 'name' => 'B', 'hours' => 2, 'price_per_hour' => 200]);
        // Sanity: salary_base = 80% of total
        $this->assertEquals(200.00, (float) $w1->salary_base);
        $this->assertEquals(320.00, (float) $w2->salary_base);
        $this->assertEquals(20.00, (float) $w1->applied_margin_pct);

        // Flip the toggle OFF at WO level
        $this->wo->update(['apply_margin' => false]);

        $w1->refresh(); $w2->refresh();

        // Both lines recomputed: salary_base = total, applied_margin_pct = 0
        $this->assertEquals(250.00, (float) $w1->salary_base);
        $this->assertEquals(400.00, (float) $w2->salary_base);
        $this->assertEquals(0.00, (float) $w1->applied_margin_pct);
        $this->assertEquals(0.00, (float) $w2->applied_margin_pct);
    }

    public function test_toggling_wo_apply_margin_back_on_recomputes_with_current_margin(): void
    {
        $w1 = WorkOrderWork::create(['work_order_id' => $this->wo->id, 'master_id' => $this->mechanic->id, 'name' => 'X', 'hours' => 1, 'price_per_hour' => 250]);
        $this->wo->update(['apply_margin' => false]);
        $w1->refresh();
        $this->assertEquals(250.00, (float) $w1->salary_base);

        // Flip back on
        $this->wo->update(['apply_margin' => true]);
        $w1->refresh();

        $this->assertEquals(200.00, (float) $w1->salary_base);
        $this->assertEquals(20.00, (float) $w1->applied_margin_pct);
    }

    public function test_changing_wo_override_margin_pct_recomputes_lines(): void
    {
        $w = WorkOrderWork::create(['work_order_id' => $this->wo->id, 'master_id' => $this->mechanic->id, 'name' => 'X', 'hours' => 1, 'price_per_hour' => 300]);
        $this->assertEquals(240.00, (float) $w->salary_base);   // 300 * 0.80 (mechanic's 20%)

        // Set an override at 40% on the WO
        $this->wo->update(['override_margin_pct' => 40]);
        $w->refresh();
        $this->assertEquals(180.00, (float) $w->salary_base);   // 300 * 0.60
        $this->assertEquals(40.00, (float) $w->applied_margin_pct);
    }
}
