<?php

namespace App\Http\Controllers\Admin\Hr;

use App\Http\Controllers\Controller;
use App\Models\Hr\SalaryRecord;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class SalaryRecordController extends Controller
{
    public function index(Request $request): JsonResponse
    {
        $query = SalaryRecord::query()->with('user');
        $query->when($request->user_id, fn($q, $v) => $q->where('user_id', $v));
        $query->when($request->year_month, fn($q, $v) => $q->where('year_month', $v));
        $query->when($request->has('status'), fn($q) => $q->where('status', $request->status));

        return $this->paginate($query->latest()->paginate($request->input('per_page', 15)));
    }

    public function store(Request $request): JsonResponse
    {
        $v = $request->validate([
            'user_id' => 'required|exists:users,id',
            'year_month' => 'required|string|max:7',
            'base_salary' => 'nullable|numeric|min:0',
            'position_salary' => 'nullable|numeric|min:0',
            'performance' => 'nullable|numeric|min:0',
            'overtime_pay' => 'nullable|numeric|min:0',
            'subsidy' => 'nullable|numeric|min:0',
            'social_insurance' => 'nullable|numeric|min:0',
            'housing_fund' => 'nullable|numeric|min:0',
            'tax' => 'nullable|numeric|min:0',
            'absence_deduction' => 'nullable|numeric|min:0',
            'other_deduction' => 'nullable|numeric|min:0',
            'actual_amount' => 'nullable|numeric|min:0',
        ]);

        return $this->success(SalaryRecord::create($v));
    }

    public function show(SalaryRecord $salaryRecord): JsonResponse
    {
        return $this->success($salaryRecord->load('user'));
    }

    public function update(Request $request, SalaryRecord $salaryRecord): JsonResponse
    {
        if ($salaryRecord->status >= 2) {
            return $this->error('已发放的工资记录不可修改', 40001);
        }
        $v = $request->validate([
            'base_salary' => 'nullable|numeric|min:0',
            'position_salary' => 'nullable|numeric|min:0',
            'performance' => 'nullable|numeric|min:0',
            'overtime_pay' => 'nullable|numeric|min:0',
            'subsidy' => 'nullable|numeric|min:0',
            'social_insurance' => 'nullable|numeric|min:0',
            'housing_fund' => 'nullable|numeric|min:0',
            'tax' => 'nullable|numeric|min:0',
            'absence_deduction' => 'nullable|numeric|min:0',
            'other_deduction' => 'nullable|numeric|min:0',
            'actual_amount' => 'nullable|numeric|min:0',
        ]);
        $salaryRecord->update($v);

        return $this->success($salaryRecord);
    }

    public function destroy(SalaryRecord $salaryRecord): JsonResponse
    {
        if ($salaryRecord->status >= 1) {
            return $this->error('已确认的工资记录不可删除', 40001);
        }
        $salaryRecord->delete();

        return $this->success(null);
    }

    public function confirm(SalaryRecord $salaryRecord): JsonResponse
    {
        if ($salaryRecord->status !== 0) {
            return $this->error('仅待确认状态可确认', 40001);
        }
        $salaryRecord->update([
            'status' => 1,
            'confirmed_at' => now(),
        ]);

        return $this->success($salaryRecord);
    }

    public function pay(SalaryRecord $salaryRecord): JsonResponse
    {
        if ($salaryRecord->status !== 1) {
            return $this->error('仅已确认状态可发放', 40001);
        }
        $salaryRecord->update([
            'status' => 2,
            'paid_at' => now(),
        ]);

        return $this->success($salaryRecord);
    }
}
