<?php

namespace Tests\Tenant;

use Mtc\Crm\Exports\EnquiryExport;
use Mtc\Crm\Models\Form;
use Mtc\Crm\Models\FormQuestion;
use Mtc\MercuryDataModels\Dealership;
use Mtc\MercuryDataModels\Enquiry;
use Tests\TenantTestCase;

class EnquiryExportTest extends TenantTestCase
{
    public function test_dealership_select_source_id_is_resolved_to_name(): void
    {
        $dealership = Dealership::factory()->create(['name' => 'Test Dealership A']);
        $form = Form::factory()->create();

        FormQuestion::factory()->create([
            'form_id' => $form->id,
            'name' => 'Dealership',
            'type' => 'dealership_select',
            'data' => ['meta' => ['options' => ['source' => 'dealerships']]],
        ]);

        Enquiry::factory()->create([
            'form_id' => $form->id,
            'details' => [
                ['question' => 'Dealership', 'answer' => $dealership->id],
            ],
        ]);

        $rows = (new EnquiryExport(['type' => $form->id]))->collection();

        $this->assertCount(1, $rows);
        $this->assertContains('Test Dealership A', $rows->first());
        $this->assertNotContains($dealership->id, $rows->first());
    }

    public function test_hidden_dealership_id_field_is_resolved_to_name(): void
    {
        $dealership = Dealership::factory()->create(['name' => 'Test Dealership B']);
        $form = Form::factory()->create();

        FormQuestion::factory()->create([
            'form_id' => $form->id,
            'name' => 'Dealership',
            'type' => 'dealership_id',
            'data' => [],
        ]);

        Enquiry::factory()->create([
            'form_id' => $form->id,
            'details' => [
                ['question' => 'Dealership', 'answer' => $dealership->id],
            ],
        ]);

        $rows = (new EnquiryExport(['type' => $form->id]))->collection();

        $this->assertCount(1, $rows);
        $this->assertContains('Test Dealership B', $rows->first());
        $this->assertNotContains($dealership->id, $rows->first());
    }

    public function test_dealership_string_id_is_resolved_to_name(): void
    {
        $dealership = Dealership::factory()->create(['name' => 'Test Dealership C']);
        $form = Form::factory()->create();

        FormQuestion::factory()->create([
            'form_id' => $form->id,
            'name' => 'Dealership',
            'type' => 'dealership_id',
            'data' => [],
        ]);

        Enquiry::factory()->create([
            'form_id' => $form->id,
            'details' => [
                ['question' => 'Dealership', 'answer' => (string) $dealership->id],
            ],
        ]);

        $rows = (new EnquiryExport(['type' => $form->id]))->collection();

        $this->assertCount(1, $rows);
        $this->assertContains('Test Dealership C', $rows->first());
    }

    public function test_plain_text_dealership_answer_is_unchanged(): void
    {
        $form = Form::factory()->create();

        FormQuestion::factory()->create([
            'form_id' => $form->id,
            'name' => 'Dealership',
            'type' => 'select_field',
            'data' => [],
        ]);

        Enquiry::factory()->create([
            'form_id' => $form->id,
            'details' => [
                ['question' => 'Dealership', 'answer' => 'some_dealership_slug'],
            ],
        ]);

        $rows = (new EnquiryExport(['type' => $form->id]))->collection();

        $this->assertCount(1, $rows);
        $this->assertContains('some_dealership_slug', $rows->first());
    }

    public function test_deleted_dealership_falls_back_to_raw_id(): void
    {
        $form = Form::factory()->create();

        FormQuestion::factory()->create([
            'form_id' => $form->id,
            'name' => 'Dealership',
            'type' => 'dealership_id',
            'data' => [],
        ]);

        Enquiry::factory()->create([
            'form_id' => $form->id,
            'details' => [
                ['question' => 'Dealership', 'answer' => 99999],
            ],
        ]);

        $rows = (new EnquiryExport(['type' => $form->id]))->collection();

        $this->assertCount(1, $rows);
        $this->assertContains(99999, $rows->first());
    }

    public function test_stored_enquiry_details_are_not_modified_after_export(): void
    {
        $dealership = Dealership::factory()->create(['name' => 'Test Dealership D']);
        $form = Form::factory()->create();

        FormQuestion::factory()->create([
            'form_id' => $form->id,
            'name' => 'Dealership',
            'type' => 'dealership_id',
            'data' => [],
        ]);

        $enquiry = Enquiry::factory()->create([
            'form_id' => $form->id,
            'details' => [
                ['question' => 'Dealership', 'answer' => $dealership->id],
            ],
        ]);

        (new EnquiryExport(['type' => $form->id]))->collection();

        $enquiry->refresh();
        $this->assertEquals($dealership->id, $enquiry->details[0]['answer']);
    }
}
