<?php

namespace App\Http\Controllers\Api\V1;

use App\Helpers\ApiResponse;
use App\Http\Controllers\Controller;
use App\Services\DepartmentService;
use Illuminate\Http\Request;

class DepartmentController extends Controller
{
    public function __construct(private DepartmentService $service) {}

    // GET /v1/departments
    public function index(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->index($request->all());
        }, '부서 목록 조회');
    }

    // GET /v1/departments/tree
    public function tree(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->tree($request->all());
        }, '부서 트리 조회');
    }

    // POST /v1/departments
    public function store(Request $request)
    {
        return ApiResponse::handle(function () use ($request) {
            return $this->service->store($request->all());
        }, '부서 생성');
    }

    // GET /v1/departments/{id}
    public function show(int $id, Request $request)
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->show($id, $request->all()); // ✅ 실제 사용하면 경고 사라짐
        }, '부서 단건 조회');
    }

    // PATCH /v1/departments/{id}
    public function update($id, Request $request)
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->update((int) $id, $request->all());
        }, '부서 수정');
    }

    // DELETE /v1/departments/{id}
    public function destroy($id, Request $request)
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->destroy((int) $id, $request->all());
        }, '부서 삭제');
    }

    // GET /v1/departments/{id}/users
    public function listUsers($id, Request $request)
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->listUsers((int) $id, $request->all());
        }, '부서 사용자 목록');
    }

    // POST /v1/departments/{id}/users
    public function attachUser($id, Request $request)
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->attachUser((int) $id, $request->all());
        }, '부서 사용자 배정');
    }

    // DELETE /v1/departments/{id}/users/{user}
    public function detachUser($id, $user, Request $request)
    {
        return ApiResponse::handle(function () use ($id, $user, $request) {
            return $this->service->detachUser((int) $id, (int) $user, $request->all());
        }, '부서 사용자 제거');
    }

    // PATCH /v1/departments/{id}/users/{user}/primary
    public function setPrimary($id, $user, Request $request)
    {
        return ApiResponse::handle(function () use ($id, $user, $request) {
            return $this->service->setPrimary((int) $id, (int) $user, $request->all());
        }, '주 부서 설정/해제');
    }

    // GET /v1/departments/{id}/permissions
    public function listPermissions($id, Request $request)
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->listPermissions((int) $id, $request->all());
        }, '부서 권한 목록');
    }

    // POST /v1/departments/{id}/permissions
    public function upsertPermissions($id, Request $request)
    {
        return ApiResponse::handle(function () use ($id, $request) {
            return $this->service->upsertPermissions((int) $id, $request->all());
        }, '부서 권한 부여/차단');
    }

    // DELETE /v1/departments/{id}/permissions/{permission}
    public function revokePermissions($id, $permission, Request $request)
    {
        return ApiResponse::handle(function () use ($id, $permission, $request) {
            return $this->service->revokePermissions((int) $id, (int) $permission, $request->all());
        }, '부서 권한 제거');
    }
}
