<?php

use App\Enums\Gender;
use App\Enums\UserStatus;
use App\Livewire\Studio\Master\User\Edit;
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->admin = User::factory()->create();
});

function mountEditUser(User $admin, User $user)
{
    return Livewire::actingAs($admin)->test(Edit::class, ['user' => $user]);
}

it('renders page successfully', function () {
    $userToEdit = User::factory()->has(Employee::factory())->create();

    mountEditUser($this->admin, $userToEdit)
        ->assertViewIs('livewire.studio.master.user.form')
        ->assertViewHas('pageTitle', 'Ubah Pegawai');
});

it('mounts form with existing user data', function () {
    $userToEdit = User::factory()->has(Employee::factory())->create();

    $component = mountEditUser($this->admin, $userToEdit);

    expect($component->form->username)->toBe($userToEdit->username);
    expect($component->form->email)->toBe($userToEdit->email);
    expect($component->form->full_name)->toBe($userToEdit->employee->full_name);
    expect($component->form->phone_number)->toBe($userToEdit->employee->phone_number);
    expect($component->form->outlet_ids)->toBe($userToEdit->outlets->pluck('id')->toArray());
});

it('prevents update when user is unauthorized', function () {
    $userToEdit = User::factory()->has(Employee::factory())->create();
    Gate::define('update user', fn () => false);

    mountEditUser($this->admin, $userToEdit)
        ->call('save')
        ->assertForbidden();
});

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

    $userToEdit = User::factory()->has(Employee::factory())->create();
    $outlets = Outlet::factory()->count(3)->create();

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

    $data = array_merge($userRaw, $employeeRaw, [
        'outlet_ids' => $outlets->pluck('id')->toArray(),
        'status' => UserStatus::ACTIVE->value,
        'gender' => $employeeRaw['gender'] ?? Gender::MALE->value,
    ]);

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

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

    $userToEdit->refresh();

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

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

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