<?php

namespace Tests\Feature;

use App\Models\AttendanceSession;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Http\UploadedFile;
use Tests\TestCase;

class AttendanceFlowTest extends TestCase
{
    use RefreshDatabase;

    public function test_staff_can_start_before_10_am(): void
    {
        $staff = User::factory()->create(['role' => 'staff']);
        Carbon::setTestNow(Carbon::parse('2026-03-26 09:30:00', 'Asia/Dhaka'));

        $this->actingAs($staff)
            ->post('/attendance/start', [
                'video_file' => UploadedFile::fake()->create('start.webm', 2048, 'video/webm'),
                'start_video_seconds' => 60,
            ])
            ->assertSessionHasNoErrors();

        $session = AttendanceSession::where('user_id', $staff->id)->firstOrFail();
        $this->assertSame('2026-03-26', $session->attendance_date->toDateString());
        $this->assertStringContainsString('attendance_videos/start/', $session->start_video_path);
    }

    public function test_staff_cannot_start_after_10_am(): void
    {
        $staff = User::factory()->create(['role' => 'staff']);
        Carbon::setTestNow(Carbon::parse('2026-03-26 10:01:00', 'Asia/Dhaka'));

        $this->actingAs($staff)
            ->post('/attendance/start', [
                'video_file' => UploadedFile::fake()->create('start.webm', 2048, 'video/webm'),
                'start_video_seconds' => 60,
            ])
            ->assertSessionHasErrors('attendance');
    }

    public function test_staff_cannot_start_or_stop_multiple_times_in_same_day(): void
    {
        $staff = User::factory()->create(['role' => 'staff']);

        Carbon::setTestNow(Carbon::parse('2026-03-26 09:00:00', 'Asia/Dhaka'));
        $this->actingAs($staff)->post('/attendance/start', [
            'video_file' => UploadedFile::fake()->create('start-first.webm', 2048, 'video/webm'),
            'start_video_seconds' => 60,
        ])->assertSessionHasNoErrors();

        Carbon::setTestNow(Carbon::parse('2026-03-26 09:05:00', 'Asia/Dhaka'));
        $this->actingAs($staff)->post('/attendance/start', [
            'video_file' => UploadedFile::fake()->create('start-second.webm', 2048, 'video/webm'),
            'start_video_seconds' => 60,
        ])->assertSessionHasErrors('attendance');

        Carbon::setTestNow(Carbon::parse('2026-03-26 18:00:00', 'Asia/Dhaka'));
        $this->actingAs($staff)->post('/attendance/stop', [
            'video_file' => UploadedFile::fake()->create('stop-first.webm', 2048, 'video/webm'),
            'stop_video_seconds' => 60,
        ])->assertSessionHasNoErrors();

        Carbon::setTestNow(Carbon::parse('2026-03-26 18:05:00', 'Asia/Dhaka'));
        $this->actingAs($staff)->post('/attendance/stop', [
            'video_file' => UploadedFile::fake()->create('stop-second.webm', 2048, 'video/webm'),
            'stop_video_seconds' => 60,
        ])->assertSessionHasErrors('attendance');
    }

    public function test_lunch_and_stop_calculates_net_minutes(): void
    {
        $staff = User::factory()->create(['role' => 'staff']);

        Carbon::setTestNow(Carbon::parse('2026-03-26 09:00:00', 'Asia/Dhaka'));
        $this->actingAs($staff)->post('/attendance/start', [
            'video_file' => UploadedFile::fake()->create('start.webm', 2048, 'video/webm'),
            'start_video_seconds' => 60,
        ]);

        Carbon::setTestNow(Carbon::parse('2026-03-26 12:00:00', 'Asia/Dhaka'));
        $this->actingAs($staff)->post('/attendance/lunch/start');

        Carbon::setTestNow(Carbon::parse('2026-03-26 12:45:00', 'Asia/Dhaka'));
        $this->actingAs($staff)->post('/attendance/lunch/end');

        Carbon::setTestNow(Carbon::parse('2026-03-26 17:00:00', 'Asia/Dhaka'));
        $this->actingAs($staff)->post('/attendance/stop', [
            'video_file' => UploadedFile::fake()->create('stop.webm', 2048, 'video/webm'),
            'stop_video_seconds' => 60,
        ]);

        $session = AttendanceSession::where('user_id', $staff->id)->firstOrFail();
        $this->assertSame(480, $session->total_work_minutes);
        $this->assertSame(45, $session->lunch_minutes);
        $this->assertSame(435, $session->net_work_minutes);
    }
}
