<?php

use App\Models\User;
use App\Enums\UserType;
use App\Models\Attendance;
use App\Models\Department;
use App\Models\EmployeeProfile;
use App\Models\LeaveRequest;
use App\Models\LeaveType;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Inertia\Testing\AssertableInertia as Assert;

uses(RefreshDatabase::class);

test('admin can view dashboard with correct metrics', function () {
    $admin = User::factory()->create(['type' => UserType::ADMIN]);


    $this->actingAs($admin)
        ->get(route('admin.dashboard'))
        ->assertStatus(200)
        ->assertInertia(fn (Assert $page) => $page
            ->component('Admin/Pages/Dashboard')
            ->has('stats')
            ->has('approvalHub')
            ->has('approvalQueue')
            ->where('stats.total_employees', 0)
        );
});

test('employee dashboard includes quick access cards for leave and manager approvals when manager', function () {
    $manager = User::factory()->create(['type' => UserType::ADMIN]);
    $employee = User::factory()->create(['type' => UserType::ADMIN]);
    $department = Department::factory()->create();
    $leaveType = LeaveType::query()->create([
        'name' => 'Annual',
        'status' => true,
    ]);

    EmployeeProfile::factory()->create([
        'user_id' => $employee->id,
        'manager_id' => $manager->id,
        'department_id' => $department->id,
    ]);

    LeaveRequest::query()->create([
        'user_id' => $manager->id,
        'leave_type_id' => $leaveType->id,
        'start_date' => now()->toDateString(),
        'end_date' => now()->toDateString(),
        'total_days' => 1,
        'status' => 'pending_manager',
    ]);

    LeaveRequest::query()->create([
        'user_id' => $employee->id,
        'leave_type_id' => $leaveType->id,
        'start_date' => now()->toDateString(),
        'end_date' => now()->toDateString(),
        'total_days' => 1,
        'status' => 'pending_manager',
    ]);

    Attendance::query()->create([
        'user_id' => $employee->id,
        'date' => now()->toDateString(),
        'clock_in' => now()->subHours(8),
        'clock_out' => now(),
        'status' => 'pending',
        'in_type' => 'manual',
        'out_type' => 'manual',
        'anomaly_status' => 'pending_manager',
    ]);

    $this->actingAs($manager)
        ->get(route('employee.dashboard'))
        ->assertStatus(200)
        ->assertInertia(fn (Assert $page) => $page
            ->component('Employee/Pages/Dashboard')
            ->has('quick_access')
            ->where('quick_access.attendance_approvals.has_access', true)
            ->where('quick_access.attendance_approvals.total_count', 1)
            ->where('quick_access.leave_approvals.has_access', true)
            ->where('quick_access.leave_approvals.total_count', 1)
        );
});
