<?php

use App\Enums\UserStatus;
use App\Livewire\Studio\Master\User\Create;
use App\Livewire\Studio\Master\User\Index;
use App\Models\Employee;
use App\Models\Outlet;
use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Gate;
use Livewire\Livewire;
use Spatie\Permission\Models\Permission;

uses(RefreshDatabase::class);

beforeEach(function () {
    $this->user = User::factory()->create();
});

function mountCreateUser(User $admin)
{
    return Livewire::actingAs($admin)->test(Create::class);
}

it('renders create user page', function () {
    mountCreateUser($this->user)
        ->assertViewIs('livewire.studio.master.user.form')
        ->assertViewHas('pageTitle', 'Tambah Pegawai');
});

it('mounts outlets correctly', function () {
    $outlets = Outlet::factory()->count(3)->create();

    $component = mountCreateUser($this->user);

    expect($component->outlets)->toHaveCount(3);
    foreach ($outlets as $outlet) {
        expect($component->outlets)->toHaveKey($outlet->id);
        expect($component->outlets[$outlet->id])->toBe($outlet->name);
    }
});

it('prevents creation when unauthorized', function () {
    Gate::define('create user', fn () => false);

    mountCreateUser($this->user)
        ->call('save')
        ->assertForbidden();
});

it('saves outlet successfully when authorized', function () {
    $permission = Permission::create(['name' => 'create user']);
    $this->user->givePermissionTo($permission);

    $outlets = Outlet::factory()->count(2)->create();

    $userRaw = User::factory()->make()->toArray();
    $employeeRaw = Employee::factory()->make()->toArray();

    $data = array_merge($userRaw, [
        'full_name' => $employeeRaw['full_name'],
        'phone_number' => $employeeRaw['phone_number'],
        'base_salary' => $employeeRaw['base_salary'],
        'birthdate' => $employeeRaw['birthdate'],
        'hire_date' => $employeeRaw['hire_date'],
        'address' => $employeeRaw['address'],
        'gender' => $employeeRaw['gender'],
        'status' => UserStatus::ACTIVE->value,
        'outlet_ids' => $outlets->pluck('id')->toArray(),
    ]);

    mountCreateUser($this->user)
        ->set('form.full_name', $data['full_name'])
        ->set('form.username', $data['username'])
        ->set('form.email', $data['email'])
        ->set('form.phone_number', $data['phone_number'])
        ->set('form.base_salary', $data['base_salary'])
        ->set('form.birthdate', $data['birthdate'])
        ->set('form.hire_date', $data['hire_date'])
        ->set('form.address', $data['address'])
        ->set('form.gender', $data['gender'])
        ->set('form.status', $data['status'])
        ->set('form.outlet_ids', $data['outlet_ids'])
        ->call('save')
        ->assertHasNoErrors()
        ->assertRedirect(Index::class);

    $user = User::where('email', $data['email'])->first();

    $this->assertDatabaseHas('users', [
        'id' => $user->id,
        'username' => $data['username'],
        'email' => $data['email'],
    ]);

    $this->assertDatabaseHas('employees', [
        'user_id' => $user->id,
        'full_name' => $data['full_name'],
        'phone_number' => $data['phone_number'],
    ]);

    expect($user->outlets->pluck('id')->toArray())->toMatchArray($data['outlet_ids']);
});
