<?php

namespace App\Services\Payroll;

use App\Models\Employee;
use Illuminate\Support\Collection;

class PayrollEngine
{
    public function generate(int $tenantId, string $period): Collection
    {
        return Employee::query()
            ->where('tenant_id', $tenantId)
            ->where('employment_status', 'active')
            ->get()
            ->map(function (Employee $employee) use ($period) {
                $salary = $employee->salary_info ?? [];
                $basic = (float) ($salary['basic'] ?? 0);
                $allowances = (float) ($salary['allowances'] ?? 0);
                $deductions = 0.0;

                return [
                    'employee_id' => $employee->id,
                    'period' => $period,
                    'basic' => $basic,
                    'allowances' => $allowances,
                    'deductions' => $deductions,
                    'net_payable' => $basic + $allowances - $deductions,
                    'status' => 'draft',
                ];
            });
    }
}
