<?php

namespace Tests\Feature\Console;

use App\Services\Attendance\AttendanceAutoPublishJobService;
use App\Services\Attendance\AttendanceDailySummaryService;
use App\Services\Attendance\AttendanceSummaryRebuildService;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Mockery;
use Tests\TestCase;

class AttendanceCommandsTest extends TestCase
{
    use RefreshDatabase;

    public function test_attendance_auto_publish_command_runs(): void
    {
        $service = Mockery::mock(AttendanceAutoPublishJobService::class);
        $service->shouldReceive('run')->once()->andReturn([
            'checked_at' => '2025-03-01 10:00:00',
            'timezone' => 'UTC',
            'published' => 0,
        ]);

        $this->app->instance(AttendanceAutoPublishJobService::class, $service);

        $this->artisan('attendance:auto-publish')->assertExitCode(0);
    }

    public function test_attendance_recalculate_summary_command_runs(): void
    {
        $service = Mockery::mock(AttendanceSummaryRebuildService::class);
        $service->shouldReceive('rebuild')->once()->andReturn(['inserted' => 3]);

        $this->app->instance(AttendanceSummaryRebuildService::class, $service);

        $this->artisan('attendance:recalculate-summary')->assertExitCode(0);
    }

    public function test_attendance_absentees_summary_command_runs(): void
    {
        $service = Mockery::mock(AttendanceDailySummaryService::class);
        $service->shouldReceive('sendAbsenteesSummary')->once()->andReturn(2);

        $this->app->instance(AttendanceDailySummaryService::class, $service);

        $this->artisan('attendance:absentees-summary')->assertExitCode(0);
    }

    public function test_attendance_lates_summary_command_runs(): void
    {
        $service = Mockery::mock(AttendanceDailySummaryService::class);
        $service->shouldReceive('sendLatesSummary')->once()->andReturn(1);

        $this->app->instance(AttendanceDailySummaryService::class, $service);

        $this->artisan('attendance:lates-summary')->assertExitCode(0);
    }
}
