<?php

namespace Tests\Feature\Controllers;

use App\Models\LeadExport;
use App\Models\LeadExportTemplate;
use Tests\DatabaseTestCase;
use Tests\UserForTenant;

class LeadExportTemplateControllerTest extends DatabaseTestCase
{
    use UserForTenant;

    private function template(array $attributes = []): LeadExportTemplate
    {
        return LeadExportTemplate::query()->create(array_merge([
            'title' => 'Template ' . uniqid(),
            'type' => 'lead',
            'is_published' => false,
        ], $attributes));
    }

    public function testIndexReturnsTemplates(): void
    {
        $this->template();
        $this->template();

        $response = $this->actingAs($this->getUser())
            ->getJson(route('lead-export-templates.index', [], false));

        $response->assertStatus(200);
        $this->assertCount(2, $response->json('data'));
    }

    public function testStoreCreatesUnpublishedTemplate(): void
    {
        $response = $this->actingAs($this->getUser())
            ->postJson(route('lead-export-templates.store', [], false), ['title' => 'Partner CRM']);

        $response->assertStatus(201);
        $this->assertSame('Partner CRM', $response->json('title'));

        $template = LeadExportTemplate::query()->where('title', 'Partner CRM')->first();
        $this->assertNotNull($template);
        $this->assertFalse($template->is_published);
    }

    public function testStoreRequiresTitle(): void
    {
        $response = $this->actingAs($this->getUser())
            ->postJson(route('lead-export-templates.store', [], false), []);

        $response->assertStatus(422);
    }

    public function testUpdatePersistsStructure(): void
    {
        $template = $this->template(['title' => 'Old']);

        $payload = [
            'is_published' => true,
            'title' => 'Updated',
            'type' => 'lead',
            'mode' => LeadExport::MODE_MAPPED,
            'field_map' => [
                ['destination' => 'email', 'label' => 'Email', 'type' => 'string'],
                ['destination' => 'vehicle.price', 'label' => 'Price', 'type' => 'float'],
            ],
            'config' => [],
        ];

        $response = $this->actingAs($this->getUser())
            ->putJson(route('lead-export-templates.update', $template->id, false), $payload);

        $response->assertStatus(200);
        $template->refresh();
        $this->assertTrue($template->is_published);
        $this->assertSame('Updated', $template->title);
        $this->assertSame(LeadExport::MODE_MAPPED, $template->mode);
        $this->assertCount(2, $template->field_map);
        $this->assertSame('vehicle.price', $template->field_map[1]['destination']);
    }

    public function testUpdateRejectsUnknownFieldType(): void
    {
        $template = $this->template();

        $response = $this->actingAs($this->getUser())
            ->putJson(route('lead-export-templates.update', $template->id, false), [
                'is_published' => false,
                'title' => 'X',
                'mode' => LeadExport::MODE_MAPPED,
                'field_map' => [['destination' => 'email', 'type' => 'nonsense']],
            ]);

        $response->assertStatus(422);
    }

    public function testDestroyRemovesTemplate(): void
    {
        $template = $this->template();

        $response = $this->actingAs($this->getUser())
            ->deleteJson(route('lead-export-templates.destroy', $template->id, false));

        $response->assertStatus(200);
        $this->assertNull(LeadExportTemplate::query()->find($template->id));
    }
}
