<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\V1\Payroll\CalculatePayrollRequest;
use App\Http\Requests\V1\Payroll\PayPayrollRequest;
use App\Http\Requests\V1\Payroll\StorePayrollRequest;
use App\Http\Requests\V1\Payroll\UpdatePayrollRequest;
use App\Http\Requests\V1\Payroll\UpdatePayrollSettingRequest;
use App\Services\PayrollService;
use Illuminate\Http\Request;

class PayrollController extends Controller
{
    public function __construct(
        private readonly PayrollService $service
    ) {}

    /**
     * 급여 목록
     */
    public function index(Request $request)
    {
        $params = $request->only([
            'year',
            'month',
            'user_id',
            'status',
            'search',
            'sort_by',
            'sort_dir',
            'per_page',
            'page',
        ]);

        $payrolls = $this->service->index($params);

        return ApiResponse::success($payrolls, __('message.fetched'));
    }

    /**
     * 특정 연월 급여 요약
     */
    public function summary(Request $request)
    {
        $year = (int) $request->input('year', date('Y'));
        $month = (int) $request->input('month', date('n'));

        $summary = $this->service->summary($year, $month);

        return ApiResponse::success($summary, __('message.fetched'));
    }

    /**
     * 급여 등록
     */
    public function store(StorePayrollRequest $request)
    {
        $payroll = $this->service->store($request->validated());

        return ApiResponse::success($payroll, __('message.created'), [], 201);
    }

    /**
     * 급여 상세
     */
    public function show(int $id)
    {
        $payroll = $this->service->show($id);

        return ApiResponse::success($payroll, __('message.fetched'));
    }

    /**
     * 급여 수정
     */
    public function update(int $id, UpdatePayrollRequest $request)
    {
        $payroll = $this->service->update($id, $request->validated());

        return ApiResponse::success($payroll, __('message.updated'));
    }

    /**
     * 급여 삭제
     */
    public function destroy(int $id)
    {
        $this->service->destroy($id);

        return ApiResponse::success(null, __('message.deleted'));
    }

    /**
     * 급여 확정
     */
    public function confirm(int $id)
    {
        $payroll = $this->service->confirm($id);

        return ApiResponse::success($payroll, __('message.payroll.confirmed'));
    }

    /**
     * 급여 지급 처리
     */
    public function pay(int $id, PayPayrollRequest $request)
    {
        $payroll = $this->service->pay($id, $request->input('withdrawal_id'));

        return ApiResponse::success($payroll, __('message.payroll.paid'));
    }

    /**
     * 일괄 확정
     */
    public function bulkConfirm(Request $request)
    {
        $year = (int) $request->input('year', date('Y'));
        $month = (int) $request->input('month', date('n'));

        $count = $this->service->bulkConfirm($year, $month);

        return ApiResponse::success(['count' => $count], __('message.payroll.bulk_confirmed'));
    }

    /**
     * 급여명세서 조회
     */
    public function payslip(int $id)
    {
        $payslip = $this->service->payslip($id);

        return ApiResponse::success($payslip, __('message.fetched'));
    }

    /**
     * 급여 일괄 계산
     */
    public function calculate(CalculatePayrollRequest $request)
    {
        $year = (int) $request->input('year');
        $month = (int) $request->input('month');
        $userIds = $request->input('user_ids');

        $payrolls = $this->service->calculate($year, $month, $userIds);

        return ApiResponse::success($payrolls, __('message.payroll.calculated'));
    }

    /**
     * 급여 설정 조회
     */
    public function getSettings()
    {
        $settings = $this->service->getSettings();

        return ApiResponse::success($settings, __('message.fetched'));
    }

    /**
     * 급여 설정 수정
     */
    public function updateSettings(UpdatePayrollSettingRequest $request)
    {
        $settings = $this->service->updateSettings($request->validated());

        return ApiResponse::success($settings, __('message.updated'));
    }
}
