<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\Attendance\CheckInRequest;
use App\Http\Requests\Attendance\CheckOutRequest;
use App\Http\Requests\Attendance\IndexRequest;
use App\Http\Requests\Attendance\MonthlyStatsRequest;
use App\Http\Requests\Attendance\StoreRequest;
use App\Http\Requests\Attendance\UpdateRequest;
use App\Services\AttendanceService;
use App\Services\ExportService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Symfony\Component\HttpFoundation\BinaryFileResponse;

class AttendanceController extends Controller
{
    public function __construct(
        private AttendanceService $service,
        private ExportService $exportService
    ) {}

    /**
     * 근태 목록 조회
     * GET /v1/attendances
     */
    public function index(IndexRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->index($request->validated());
        }, __('message.fetched'));
    }

    /**
     * 근태 상세 조회
     * GET /v1/attendances/{id}
     */
    public function show(int $id): JsonResponse
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->show($id);
        }, __('message.fetched'));
    }

    /**
     * 근태 등록
     * POST /v1/attendances
     */
    public function store(StoreRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->store($request->validated());
        }, __('message.created'));
    }

    /**
     * 근태 수정
     * PATCH /v1/attendances/{id}
     */
    public function update(int $id, UpdateRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->update($id, $request->validated());
        }, __('message.updated'));
    }

    /**
     * 근태 삭제
     * DELETE /v1/attendances/{id}
     */
    public function destroy(int $id): JsonResponse
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->destroy($id);
        }, __('message.deleted'));
    }

    /**
     * 근태 일괄 삭제
     * POST /v1/attendances/bulk-delete
     */
    public function bulkDelete(Request $request): JsonResponse
    {
        $request->validate([
            'ids' => 'required|array|min:1',
            'ids.*' => 'integer|min:1',
        ]);

        return ApiResponse::handle(function () use ($request) {
            return $this->service->bulkDelete($request->input('ids'));
        }, __('message.bulk_deleted'));
    }

    /**
     * 출근 기록 (체크인)
     * POST /v1/attendances/check-in
     */
    public function checkIn(CheckInRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->checkIn($request->validated());
        }, __('message.created'));
    }

    /**
     * 퇴근 기록 (체크아웃)
     * POST /v1/attendances/check-out
     */
    public function checkOut(CheckOutRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->checkOut($request->validated());
        }, __('message.updated'));
    }

    /**
     * 월간 통계 조회
     * GET /v1/attendances/monthly-stats
     */
    public function monthlyStats(MonthlyStatsRequest $request): JsonResponse
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->monthlyStats($request->validated());
        }, __('message.fetched'));
    }

    /**
     * 근태 엑셀 내보내기
     * GET /v1/attendances/export
     */
    public function export(Request $request): BinaryFileResponse
    {
        $params = $request->only([
            'user_id',
            'date',
            'date_from',
            'date_to',
            'status',
            'department_id',
            'sort_by',
            'sort_dir',
        ]);

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

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