<?php

declare(strict_types=1);

namespace Tests\Feature;

use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Maestro\Core\Database\Seeders\UserSeeder;
use Maestro\Core\Models\MaestroAppSetting;
use Tests\Fixtures\AuthTestUser;
use Tests\TestCase;

final class AppearanceThemeSuperadminApiTest extends TestCase
{
    use RefreshDatabase;

    protected function setUp(): void
    {
        parent::setUp();
        $this->withoutMiddleware(VerifyCsrfToken::class);
        $this->seed([UserSeeder::class]);
    }

    public function test_superadmin_can_store_custom_theme_colors(): void
    {
        $user = AuthTestUser::query()->where('email', 'demo@example.com')->first();
        self::assertNotNull($user);

        $response = $this->actingAs($user, 'web')->patchJson('/api/superadmin/app-settings', [
            'theme' => [
                'primary' => 'custom',
                'primaryCustom' => '#ff0000',
                'surface' => 'custom',
                'surfaceCustom' => '#64748b',
            ],
        ]);

        $response->assertOk();
        $response->assertJsonPath('theme.primary', 'custom');
        $response->assertJsonPath('theme.primaryCustom', '#ff0000');
        $response->assertJsonPath('theme.surface', 'custom');
        $response->assertJsonPath('theme.surfaceCustom', '#64748b');

        $settings = $this->getJson('/api/app-settings');
        $settings->assertOk();
        $settings->assertJsonPath('theme.primaryCustom', '#ff0000');
        $settings->assertJsonPath('theme.surfaceCustom', '#64748b');

        $row = MaestroAppSetting::instance()->fresh();
        self::assertNotNull($row);
        self::assertIsArray($row->theme);
        self::assertSame('custom', $row->theme['primary']);
        self::assertSame('#ff0000', $row->theme['primaryCustom']);
    }

    public function test_invalid_custom_hex_returns_validation_error(): void
    {
        $user = AuthTestUser::query()->where('email', 'demo@example.com')->first();
        self::assertNotNull($user);

        $response = $this->actingAs($user, 'web')->patchJson('/api/superadmin/app-settings', [
            'theme' => [
                'primary' => 'custom',
                'primaryCustom' => 'not-a-color',
            ],
        ]);

        $response->assertUnprocessable();
        $response->assertJsonValidationErrors(['theme.primaryCustom']);
    }

    public function test_preset_primary_without_custom_fields_still_works(): void
    {
        $user = AuthTestUser::query()->where('email', 'demo@example.com')->first();
        self::assertNotNull($user);

        $response = $this->actingAs($user, 'web')->patchJson('/api/superadmin/app-settings', [
            'theme' => [
                'primary' => 'blue',
                'surface' => 'slate',
                'darkTheme' => false,
            ],
        ]);

        $response->assertOk();
        $response->assertJsonPath('theme.primary', 'blue');
        $response->assertJsonPath('theme.surface', 'slate');
        $response->assertJsonPath('theme.darkTheme', false);
    }

    public function test_three_digit_hex_is_normalized_to_six_digits(): void
    {
        $user = AuthTestUser::query()->where('email', 'demo@example.com')->first();
        self::assertNotNull($user);

        $response = $this->actingAs($user, 'web')->patchJson('/api/superadmin/app-settings', [
            'theme' => [
                'primary' => 'custom',
                'primaryCustom' => '#f00',
            ],
        ]);

        $response->assertOk();
        $response->assertJsonPath('theme.primaryCustom', '#ff0000');
    }
}
