<?php

namespace Modules\HR\Repositories;

use Modules\Core\Enums\StatusEnum;
use Modules\HR\DTO\HrSettingFilter;
use Modules\HR\Enums\SalaryRevisionStatusEnum;
use Modules\HR\Models\SalaryRevision;
use Modules\HR\Models\SalaryStructure;

class SalaryRevisionRepository
{
    public function all(HrSettingFilter $filter, $columns = ['*'], $relations = [])
    {
        return SalaryRevision::query()
            ->with($relations)
            ->filterSalaryRevisionStatus($filter->salary_revision_status)
            ->select($columns)
            ->paginate($filter->perPage ?? 15);
    }

    public function find($id)
    {
        return SalaryRevision::findOrFail($id);
    }


    public function create(array $data)
    {
        return SalaryRevision::create([
            'employee_id'             => $data['employee_id'],
            'revision_type'           => $data['revision_type'],
            'effective_date'          => $data['effective_date'],
            'reason'                  => $data['reason'] ?? null,

            // Snapshot (apply করার আগের values)
            'old_basic_salary'        => $data['old_basic_salary'] ?? null,
            'old_salary_structure_id' => $data['old_salary_structure_id'] ?? null,
            'old_designation_id'      => $data['old_designation_id'] ?? null,

            // Calculated new values
            'new_basic_salary'        => $data['new_basic_salary'] ?? null,
            'new_designation_id'      => $data['new_designation_id'] ?? null,

            // Type-specific
            'revision_amount'         => $data['revision_amount'] ?? null,
            'adjustment_direction'    => $data['adjustment_direction'] ?? null,
            'clone_salary_structure'  => $data['clone_salary_structure'] ?? false,

            'salary_revision_status'  => $data['salary_revision_status'] ?? SalaryRevisionStatusEnum::PENDING,


        ]);
    }

    public function approve(SalaryRevision $salaryRevision, array $data)
    {
        $salaryRevision->update([
            'salary_revision_status' => SalaryRevisionStatusEnum::APPROVED,
            'approved_by'            => auth()->id(),
            'approved_at'            => now(),
            'approval_comment'       => $data['approval_comment'] ?? null,
        ]);

        return $salaryRevision->fresh();
    }

    public function reject(SalaryRevision $salaryRevision, array $data)
    {
        $salaryRevision->update([
            'salary_revision_status' => SalaryRevisionStatusEnum::REJECTED,
            'approved_by'            => auth()->id(),
            'approved_at'            => now(),
            'approval_comment'       => $data['approval_comment'] ?? null,
        ]);

        return $salaryRevision->fresh();
    }


    public function cancel(SalaryRevision $revision, array $data): SalaryRevision
    {
        $revision->update([
            'salary_revision_status' => SalaryRevisionStatusEnum::CANCELED,
            'approval_comment'       => $data['approval_comment'] ?? null,
        ]);

        return $revision->fresh();
    }


    public function apply(SalaryRevision $revision, ?SalaryStructure $newStructure): SalaryRevision
    {
        $revision->update([
            'salary_revision_status' => SalaryRevisionStatusEnum::APPLIED,
            'cloned_structure_id'    => $newStructure?->id,
            'applied_at'             => now(),
        ]);

        return $revision->fresh();
    }
}
