<?php

namespace Tests\Feature\Api\V1\Staff;

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

class StaffControllerTest extends TestCase
{
    use RefreshDatabase;

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

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

        $response->assertForbidden();
    }

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

        $staffUser = $this->createUser('teacher', 'staff@example.com');
        Staff::query()->create([
            'user_id' => $staffUser->id,
            'firstname' => 'Staff',
            'lastname' => 'User',
            'email' => 'staff@example.com',
            'phone' => '5555555555',
            'role_name' => 'teacher',
            'active_role' => 'teacher',
            'school_year' => '2025-2026',
        ]);

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

        $response->assertOk();
        $response->assertJsonPath('data.staff.0.email', 'staff@example.com');
    }

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

        $user = $this->createUser('teacher', 'newstaff@example.com');

        $response = $this->postJson('/api/v1/staff', [
            'user_id' => $user->id,
            'firstname' => 'New',
            'lastname' => 'Staff',
            'email' => 'newstaff@example.com',
            'phone' => '5555555555',
            'role_name' => 'teacher',
            'school_year' => '2025-2026',
        ]);

        $response->assertStatus(201);
        $this->assertDatabaseHas('staff', [
            'user_id' => $user->id,
            'email' => 'newstaff@example.com',
        ]);
    }

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

        $response = $this->postJson('/api/v1/staff', [
            'firstname' => 'New',
        ]);

        $response->assertStatus(422);
    }

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

        $user = $this->createUser('teacher', 'updatestaff@example.com');
        $staff = Staff::query()->create([
            'user_id' => $user->id,
            'firstname' => 'Old',
            'lastname' => 'Name',
            'email' => 'updatestaff@example.com',
            'phone' => '5555555555',
            'role_name' => 'teacher',
            'active_role' => 'teacher',
            'school_year' => '2025-2026',
        ]);

        $response = $this->patchJson('/api/v1/staff/' . $staff->id, [
            'firstname' => 'Updated',
        ]);

        $response->assertOk();
        $this->assertDatabaseHas('staff', [
            'id' => $staff->id,
            'firstname' => 'Updated',
        ]);
    }

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

        $user = $this->createUser('teacher', 'deletestaff@example.com');
        $staff = Staff::query()->create([
            'user_id' => $user->id,
            'firstname' => 'Delete',
            'lastname' => 'Staff',
            'email' => 'deletestaff@example.com',
            'phone' => '5555555555',
            'role_name' => 'teacher',
            'active_role' => 'teacher',
            'school_year' => '2025-2026',
        ]);

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

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

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

        $user = User::query()->create([
            'firstname' => 'Test',
            'lastname' => 'User',
            'email' => $email ?? ($roleName . '@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' => $user->id,
            'role_id' => $roleId,
        ]);

        return $user;
    }
}
