<?php

namespace App\Modules\HR\Shifts\Repositories;

use App\Modules\HR\Employees\Models\Employee;
use App\Modules\HR\Shifts\Models\Shift;

class ShiftStatisticsRepository
{
    public function activeShiftsCount(int $tenantId): int
    {
        return Shift::query()
            ->where('tenant_id', $tenantId)
            ->where('is_active', true)
            ->count();
    }

    public function employeesWithoutActiveShiftCount(int $tenantId): int
    {
        return Employee::query()
            ->where('tenant_id', $tenantId)
            ->where('is_active', true)
            ->whereDoesntHave('shiftAssignments', function ($q): void {
                $q->where('is_active', true)
                    ->where('starts_at', '<=', now())
                    ->where(function ($q2): void {
                        $q2->whereNull('ends_at')->orWhere('ends_at', '>=', now());
                    })
                    ->whereHas('shift', fn ($s) => $s->where('is_active', true));
            })
            ->count();
    }
}
