<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Http\Requests\Employee\IndexRequest;
use App\Http\Requests\Employee\StoreRequest;
use App\Http\Requests\Employee\UpdateRequest;
use App\Services\EmployeeService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class EmployeeController extends Controller
{
    public function __construct(private EmployeeService $service) {}

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

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

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

    /**
     * 사원 수정
     * PATCH /v1/employees/{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/employees/{id}
     */
    public function destroy(int $id): JsonResponse
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->destroy($id);
        }, __('message.deleted'));
    }

    /**
     * 사원 일괄 삭제
     * POST /v1/employees/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'));
    }

    /**
     * 사원 통계
     * GET /v1/employees/stats
     */
    public function stats(): JsonResponse
    {
        return ApiResponse::handle(function () {
            return $this->service->stats();
        }, __('message.fetched'));
    }

    /**
     * 시스템 계정 생성
     * POST /v1/employees/{id}/create-account
     */
    public function createAccount(int $id, Request $request): JsonResponse
    {
        $request->validate([
            'password' => 'required|string|min:8',
        ]);

        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->createAccount($id, $request->input('password'));
        }, __('employee.account_created'));
    }

    /**
     * 시스템 계정 해제 (로그인 불가, 사원 정보 유지)
     * POST /v1/employees/{id}/revoke-account
     */
    public function revokeAccount(int $id): JsonResponse
    {
        return ApiResponse::handle(function () use ($id) {
            return $this->service->revokeAccount($id);
        }, __('employee.account_revoked'));
    }
}
