<?php

namespace Tests\Unit\Services\Users;

use App\Models\User;
use App\Services\Users\UserEventService;
use App\Services\Users\UserManagementService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Mockery;
use Tests\TestCase;

class UserManagementServiceTest extends TestCase
{
    use RefreshDatabase;

    public function test_create_persists_user_and_role_and_sends_welcome(): void
    {
        DB::table('roles')->insert([
            'id' => 1,
            'name' => 'Parent',
            'slug' => 'parent',
            'description' => 'Parent role',
            'dashboard_route' => 'parent/dashboard',
            'priority' => 1,
            'is_active' => 1,
        ]);

        $eventService = Mockery::mock(UserEventService::class);
        $eventService->shouldReceive('handleNewAccountAdded')
            ->once()
            ->andReturn(true);

        $service = new UserManagementService($eventService);

        $result = $service->create([
            'firstname' => 'New',
            'lastname' => 'User',
            'cellphone' => '5554443333',
            'email' => 'new.user@example.com',
            'address_street' => '123 Main',
            'city' => 'City',
            'state' => 'ST',
            'zip' => '12345',
            'accept_school_policy' => true,
            'role_id' => 1,
            'password' => 'secret123',
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        $this->assertTrue($result['ok']);
        $this->assertInstanceOf(User::class, $result['user']);
        $this->assertDatabaseHas('users', [
            'email' => 'new.user@example.com',
            'firstname' => 'New',
            'lastname' => 'User',
        ]);
        $this->assertDatabaseHas('user_roles', [
            'user_id' => $result['user']->id,
            'role_id' => 1,
        ]);
    }

    public function test_update_changes_user_fields(): void
    {
        DB::table('users')->insert([
            'id' => 1,
            'school_id' => 1,
            'firstname' => 'Test',
            'lastname' => 'User',
            'cellphone' => '5555555555',
            'email' => 'test@example.com',
            'address_street' => '123 Main',
            'city' => 'City',
            'state' => 'ST',
            'zip' => '12345',
            'accept_school_policy' => 1,
            'is_verified' => 1,
            'status' => 'Active',
            'is_suspended' => 0,
            'failed_attempts' => 0,
            'password' => bcrypt('secret'),
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        $eventService = Mockery::mock(UserEventService::class);
        $service = new UserManagementService($eventService);

        $result = $service->update(1, [
            'firstname' => 'Updated',
            'email' => 'updated@example.com',
            'accept_school_policy' => false,
        ]);

        $this->assertTrue($result['ok']);
        $this->assertDatabaseHas('users', [
            'id' => 1,
            'firstname' => 'Updated',
            'email' => 'updated@example.com',
            'accept_school_policy' => 0,
        ]);
    }

    public function test_delete_removes_user_and_roles(): void
    {
        DB::table('users')->insert([
            'id' => 1,
            'school_id' => 1,
            'firstname' => 'Test',
            'lastname' => 'User',
            'cellphone' => '5555555555',
            'email' => 'test@example.com',
            'address_street' => '123 Main',
            'city' => 'City',
            'state' => 'ST',
            'zip' => '12345',
            'accept_school_policy' => 1,
            'is_verified' => 1,
            'status' => 'Active',
            'is_suspended' => 0,
            'failed_attempts' => 0,
            'password' => bcrypt('secret'),
            'semester' => 'Fall',
            'school_year' => '2025-2026',
        ]);

        DB::table('roles')->insert([
            'id' => 1,
            'name' => 'Parent',
            'slug' => 'parent',
            'description' => 'Parent role',
            'dashboard_route' => 'parent/dashboard',
            'priority' => 1,
            'is_active' => 1,
        ]);

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

        $eventService = Mockery::mock(UserEventService::class);
        $service = new UserManagementService($eventService);

        $result = $service->delete(1);

        $this->assertTrue($result['ok']);
        $this->assertDatabaseMissing('users', ['id' => 1]);
        $this->assertDatabaseMissing('user_roles', ['user_id' => 1]);
    }
}
