<?php

namespace Tests\Feature\Controllers;

use App\Models\NotificationTemplate;
use Tests\DatabaseTestCase;
use Tests\UserForTenant;

class NotificationTemplateControllerTest extends DatabaseTestCase
{
    use UserForTenant;

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

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

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

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

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

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

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

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

        $response->assertStatus(422);
    }

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

        $payload = [
            'is_published' => true,
            'title' => 'Updated',
            'type' => 'vehicle',
            'field_map' => [
                ['source' => 'stock_id', 'destination' => 'uuid', 'type' => 'string'],
                ['source' => 'images', 'destination' => 'images', 'type' => 'image_list'],
            ],
            'validation' => ['uuid' => ['required', 'string']],
            'config' => ['images' => ['url_key' => 'href']],
        ];

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

        $response->assertStatus(200);
        $template->refresh();
        $this->assertTrue($template->is_published);
        $this->assertSame('Updated', $template->title);
        $this->assertCount(2, $template->field_map);
        $this->assertSame(['uuid' => ['required', 'string']], $template->validation);
        $this->assertSame('href', $template->config['images']['url_key']);
    }

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

        $response = $this->actingAs($this->getUser())
            ->putJson(route('notification-templates.update', $template->id, false), [
                'is_published' => false,
                'title' => 'X',
                'field_map' => [['source' => 'a', 'destination' => 'b', 'type' => 'nonsense']],
            ]);

        $response->assertStatus(422);
    }

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

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

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