<?php

namespace App\Http\Controllers\Api\Admin\HR;

use App\Http\Controllers\Controller;
use App\Services\HR\AttendanceRequestService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response;

class AttendanceRequestController extends Controller
{
    public function __construct(
        private AttendanceRequestService $requestService
    ) {}

    /**
     * 신청 목록 (HTMX → HTML / 일반 → JSON)
     */
    public function index(Request $request): JsonResponse|Response
    {
        $requests = $this->requestService->getRequests(
            $request->all(),
            $request->integer('per_page', 20)
        );

        if ($request->header('HX-Request')) {
            return response(view('hr.attendances.partials.requests', compact('requests')));
        }

        return response()->json([
            'success' => true,
            'data' => $requests->items(),
            'meta' => [
                'current_page' => $requests->currentPage(),
                'last_page' => $requests->lastPage(),
                'per_page' => $requests->perPage(),
                'total' => $requests->total(),
            ],
        ]);
    }

    /**
     * 신청 등록
     */
    public function store(Request $request): JsonResponse
    {
        $validated = $request->validate([
            'user_id' => 'required|integer|exists:users,id',
            'request_type' => 'required|string|in:vacation,businessTrip,remote,fieldWork',
            'start_date' => 'required|date',
            'end_date' => 'required|date|after_or_equal:start_date',
            'reason' => 'nullable|string|max:1000',
            'json_details' => 'nullable|array',
        ]);

        try {
            $attendanceRequest = $this->requestService->storeRequest($validated);

            return response()->json([
                'success' => true,
                'message' => '근태 신청이 등록되었습니다.',
                'data' => $attendanceRequest,
            ], 201);
        } catch (\Throwable $e) {
            report($e);

            return response()->json([
                'success' => false,
                'message' => '신청 등록 중 오류가 발생했습니다.',
                'error' => config('app.debug') ? $e->getMessage() : null,
            ], 500);
        }
    }

    /**
     * 승인
     */
    public function approve(Request $request, int $id): JsonResponse
    {
        try {
            $attendanceRequest = $this->requestService->approve($id);

            if (! $attendanceRequest) {
                return response()->json([
                    'success' => false,
                    'message' => '대기 중인 신청을 찾을 수 없습니다.',
                ], 404);
            }

            return response()->json([
                'success' => true,
                'message' => '승인 처리되었습니다. 근태 레코드가 자동 생성되었습니다.',
                'data' => $attendanceRequest,
            ]);
        } catch (\Throwable $e) {
            report($e);

            return response()->json([
                'success' => false,
                'message' => '승인 처리 중 오류가 발생했습니다.',
                'error' => config('app.debug') ? $e->getMessage() : null,
            ], 500);
        }
    }

    /**
     * 반려
     */
    public function reject(Request $request, int $id): JsonResponse
    {
        $validated = $request->validate([
            'reject_reason' => 'nullable|string|max:1000',
        ]);

        try {
            $attendanceRequest = $this->requestService->reject($id, $validated['reject_reason'] ?? null);

            if (! $attendanceRequest) {
                return response()->json([
                    'success' => false,
                    'message' => '대기 중인 신청을 찾을 수 없습니다.',
                ], 404);
            }

            return response()->json([
                'success' => true,
                'message' => '반려 처리되었습니다.',
                'data' => $attendanceRequest,
            ]);
        } catch (\Throwable $e) {
            report($e);

            return response()->json([
                'success' => false,
                'message' => '반려 처리 중 오류가 발생했습니다.',
                'error' => config('app.debug') ? $e->getMessage() : null,
            ], 500);
        }
    }
}
