<?php

namespace Tests\Feature\Api\V1\Settings;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Laravel\Sanctum\Sanctum;
use Tests\TestCase;

class PreferencesControllerTest extends TestCase
{
    use RefreshDatabase;

    public function test_show_returns_defaults(): void
    {
        $user = $this->createUser('teacher');
        Sanctum::actingAs($user);

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

        $response->assertOk();
        $response->assertJsonPath('data.preferences.receive_email_notifications', true);
        $response->assertJsonStructure(['data' => ['options' => ['style_options', 'menu_options']]]);
    }

    public function test_store_creates_preferences(): void
    {
        $user = $this->createUser('teacher');
        Sanctum::actingAs($user);

        $response = $this->postJson('/api/v1/preferences', [
            'receive_email_notifications' => true,
            'receive_sms_notifications' => false,
            'theme' => 'dark',
            'language' => 'en',
            'style_color' => 'blue',
            'menu_color' => 'white',
        ]);

        $response->assertOk();
        $this->assertDatabaseHas('user_preferences', [
            'user_id' => $user->id,
            'notification_email' => 1,
            'notification_sms' => 0,
        ]);
    }

    public function test_list_requires_admin(): void
    {
        $user = $this->createUser('teacher');
        Sanctum::actingAs($user);

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

        $response->assertForbidden();
    }

    public function test_list_returns_rows_for_admin(): void
    {
        $admin = $this->createUser('admin');
        Sanctum::actingAs($admin);

        DB::table('user_preferences')->insert([
            'user_id' => $admin->id,
            'notification_email' => 1,
            'notification_sms' => 1,
            'theme' => 'light',
            'language' => 'en',
        ]);

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

        $response->assertOk();
        $response->assertJsonPath('data.preferences.0.user_id', $admin->id);
    }

    public function test_update_for_user_by_admin(): void
    {
        $admin = $this->createUser('admin');
        $user = $this->createUser('teacher', 2);
        Sanctum::actingAs($admin);

        $response = $this->putJson('/api/v1/preferences/2', [
            'receive_sms_notifications' => false,
        ]);

        $response->assertOk();
        $this->assertDatabaseHas('user_preferences', [
            'user_id' => $user->id,
            'notification_sms' => 0,
        ]);
    }

    public function test_destroy_requires_admin(): void
    {
        $user = $this->createUser('teacher');
        Sanctum::actingAs($user);

        $response = $this->deleteJson('/api/v1/preferences/' . $user->id);

        $response->assertForbidden();
    }

    public function test_destroy_deletes_preferences(): void
    {
        $admin = $this->createUser('admin');
        Sanctum::actingAs($admin);

        DB::table('user_preferences')->insert([
            'user_id' => $admin->id,
            'notification_email' => 1,
            'notification_sms' => 1,
        ]);

        $response = $this->deleteJson('/api/v1/preferences/' . $admin->id);

        $response->assertOk();
        $this->assertDatabaseMissing('user_preferences', [
            'user_id' => $admin->id,
        ]);
    }

    public function test_store_validates_payload(): void
    {
        $user = $this->createUser('teacher');
        Sanctum::actingAs($user);

        $response = $this->postJson('/api/v1/preferences', [
            'theme' => 'invalid',
        ]);

        $response->assertStatus(422);
        $response->assertJsonStructure(['message', 'errors']);
    }

    private function createUser(string $roleName, int $id = 1): User
    {
        $roleId = DB::table('roles')->insertGetId([
            'name' => $roleName,
            'priority' => 1,
            'is_active' => 1,
        ]);

        DB::table('users')->insert([
            'id' => $id,
            'firstname' => 'Test',
            'lastname' => 'User',
            'email' => $roleName . $id . '@example.com',
            'cellphone' => '5555555555',
            'address_street' => '123 Main',
            'city' => 'City',
            'state' => 'ST',
            'zip' => '12345',
            'accept_school_policy' => 1,
            'status' => 'Active',
            'password' => bcrypt('secret'),
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        DB::table('user_roles')->insert([
            'user_id' => $id,
            'role_id' => $roleId,
        ]);

        return User::query()->findOrFail($id);
    }
}
