<?php

namespace Tests\Feature;

use App\Models\Setting;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;

class SettingsApiTest extends TestCase
{
    use RefreshDatabase;

    public function test_settings_api_returns_branding_language_and_contact(): void
    {
        Setting::create(['key' => 'default_language', 'value' => 'km', 'value_km' => 'km', 'type' => 'text', 'group' => 'general', 'label' => 'Default Language', 'order' => 1]);
        Setting::create(['key' => 'site_logo', 'value' => 'settings/logo.png', 'value_km' => null, 'type' => 'image', 'group' => 'general', 'label' => 'Site Logo', 'order' => 2]);
        Setting::create(['key' => 'favicon', 'value' => 'settings/favicon.png', 'value_km' => null, 'type' => 'image', 'group' => 'general', 'label' => 'Favicon', 'order' => 3]);

        Setting::create(['key' => 'company_name', 'value' => '360Monica', 'value_km' => '360Monica', 'type' => 'text', 'group' => 'contact', 'label' => 'Company Name', 'order' => 1]);
        Setting::create(['key' => 'contact_email', 'value' => 'contact@360monica.com', 'value_km' => 'contact@360monica.com', 'type' => 'email', 'group' => 'contact', 'label' => 'Contact Email', 'order' => 2]);
        Setting::create(['key' => 'primary_phone', 'value' => '+855 12 345 678', 'value_km' => '+855 12 345 678', 'type' => 'text', 'group' => 'contact', 'label' => 'Primary Phone', 'order' => 3]);

        $response = $this->getJson('/api/v1/settings');

        $response->assertOk();
        $response->assertJsonPath('success', true);
        $response->assertJsonPath('data.language.default', 'km');
        $response->assertJsonPath('data.branding.logo.path', 'settings/logo.png');
        $response->assertJsonPath('data.branding.icon.path', 'settings/favicon.png');
        $response->assertJsonPath('data.contact.company_name', '360Monica');
        $response->assertJsonPath('data.contact.emails.contact', 'contact@360monica.com');
        $response->assertJsonPath('data.contact.phones.primary', '+855 12 345 678');
    }

    public function test_settings_api_localizes_values_when_lang_km(): void
    {
        Setting::create(['key' => 'default_language', 'value' => 'en', 'value_km' => 'km', 'type' => 'text', 'group' => 'general', 'label' => 'Default Language', 'order' => 1]);
        Setting::create(['key' => 'address_city', 'value' => 'Phnom Penh', 'value_km' => 'ភ្នំពេញ', 'type' => 'text', 'group' => 'contact', 'label' => 'City', 'order' => 2]);

        $response = $this->getJson('/api/v1/settings?lang=km');

        $response->assertOk();
        $response->assertJsonPath('data.language.current', 'km');
        $response->assertJsonPath('data.contact.address.city', 'ភ្នំពេញ');
    }

    public function test_settings_about_api_returns_about_fields(): void
    {
        Setting::create(['key' => 'default_language', 'value' => 'km', 'value_km' => 'km', 'type' => 'text', 'group' => 'general', 'label' => 'Default Language', 'order' => 1]);
        Setting::create(['key' => 'about_title', 'value' => 'About Us', 'value_km' => 'អំពីយើង', 'type' => 'text', 'group' => 'about', 'label' => 'About Title', 'order' => 1]);
        Setting::create(['key' => 'about_content', 'value' => 'Welcome', 'value_km' => 'សូមស្វាគមន៍', 'type' => 'textarea', 'group' => 'about', 'label' => 'About Content', 'order' => 2]);
        Setting::create(['key' => 'about_image', 'value' => 'settings/about.png', 'value_km' => null, 'type' => 'image', 'group' => 'about', 'label' => 'About Image', 'order' => 3]);

        $response = $this->getJson('/api/v1/settings/about?lang=km');

        $response->assertOk();
        $response->assertJsonPath('success', true);
        $response->assertJsonPath('data.about.title', 'អំពីយើង');
        $response->assertJsonPath('data.about.content', 'សូមស្វាគមន៍');
        $response->assertJsonPath('data.about.image.path', 'settings/about.png');
    }
}
