<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\V1\Salary\BulkUpdateStatusRequest;
use App\Http\Requests\V1\Salary\StoreSalaryRequest;
use App\Http\Requests\V1\Salary\UpdateSalaryRequest;
use App\Services\ExportService;
use App\Services\SalaryService;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class SalaryController extends Controller
{
    public function __construct(
        private readonly SalaryService $service,
        private readonly ExportService $exportService
    ) {}

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

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

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

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

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

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

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

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

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

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

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

    /**
     * 급여 상태 변경 (지급완료/지급예정)
     */
    public function updateStatus(int $id, Request $request)
    {
        $status = $request->input('status', 'completed');
        $salary = $this->service->updateStatus($id, $status);

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

    /**
     * 급여 일괄 상태 변경
     */
    public function bulkUpdateStatus(BulkUpdateStatusRequest $request)
    {
        $count = $this->service->bulkUpdateStatus(
            $request->input('ids'),
            $request->input('status')
        );

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

    /**
     * 급여 통계 조회
     */
    public function statistics(Request $request)
    {
        $params = $request->only([
            'year',
            'month',
            'start_date',
            'end_date',
        ]);

        $stats = $this->service->getStatistics($params);

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

    /**
     * 급여 엑셀 내보내기
     * GET /v1/salaries/export
     */
    public function export(Request $request): BinaryFileResponse
    {
        $params = $request->only([
            'search',
            'year',
            'month',
            'status',
            'employee_id',
            'start_date',
            'end_date',
            'sort_by',
            'sort_dir',
        ]);

        $exportData = $this->service->getExportData($params);
        $filename = '급여현황_'.date('Ymd_His');

        return $this->exportService->download(
            $exportData['data'],
            $exportData['headings'],
            $filename,
            '급여현황'
        );
    }
}
