<?php

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

use App\Http\Controllers\Controller;
use App\Http\Requests\ProjectManagement\StoreProjectRequest;
use App\Http\Requests\ProjectManagement\UpdateProjectRequest;
use App\Services\ProjectManagement\ProjectService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class ProjectController extends Controller
{
    public function __construct(
        private readonly ProjectService $projectService
    ) {}

    /**
     * 프로젝트 목록 (HTMX용)
     */
    public function index(Request $request): View|JsonResponse
    {
        $filters = $request->only(['search', 'status', 'trashed', 'sort_by', 'sort_direction']);
        $projects = $this->projectService->getProjects($filters, 15);

        // HTMX 요청이면 HTML 파셜 반환
        if ($request->header('HX-Request')) {
            return view('project-management.projects.partials.table', compact('projects'));
        }

        return response()->json([
            'success' => true,
            'data' => $projects,
        ]);
    }

    /**
     * 활성 프로젝트 목록 (드롭다운용)
     */
    public function dropdown(): JsonResponse
    {
        $projects = $this->projectService->getActiveProjects();

        return response()->json([
            'success' => true,
            'data' => $projects,
        ]);
    }

    /**
     * 프로젝트 통계
     */
    public function stats(): JsonResponse
    {
        $stats = $this->projectService->getProjectStats();

        return response()->json([
            'success' => true,
            'data' => $stats,
        ]);
    }

    /**
     * 대시보드 요약
     */
    public function dashboard(): JsonResponse
    {
        $summary = $this->projectService->getDashboardSummary();

        return response()->json([
            'success' => true,
            'data' => $summary,
        ]);
    }

    /**
     * 프로젝트 상세 조회
     */
    public function show(int $id): JsonResponse
    {
        $project = $this->projectService->getProjectById($id, true);

        if (! $project) {
            return response()->json([
                'success' => false,
                'message' => '프로젝트를 찾을 수 없습니다.',
            ], 404);
        }

        return response()->json([
            'success' => true,
            'data' => $project,
        ]);
    }

    /**
     * 프로젝트 생성
     */
    public function store(StoreProjectRequest $request): JsonResponse
    {
        $project = $this->projectService->createProject($request->validated());

        return response()->json([
            'success' => true,
            'message' => '프로젝트가 생성되었습니다.',
            'data' => $project,
        ]);
    }

    /**
     * 프로젝트 수정
     */
    public function update(UpdateProjectRequest $request, int $id): JsonResponse
    {
        $this->projectService->updateProject($id, $request->validated());

        return response()->json([
            'success' => true,
            'message' => '프로젝트가 수정되었습니다.',
        ]);
    }

    /**
     * 프로젝트 삭제 (Soft Delete)
     */
    public function destroy(int $id): JsonResponse
    {
        $this->projectService->deleteProject($id);

        return response()->json([
            'success' => true,
            'message' => '프로젝트가 삭제되었습니다.',
        ]);
    }

    /**
     * 프로젝트 복원
     */
    public function restore(int $id): JsonResponse
    {
        $this->projectService->restoreProject($id);

        return response()->json([
            'success' => true,
            'message' => '프로젝트가 복원되었습니다.',
        ]);
    }

    /**
     * 프로젝트 영구 삭제
     */
    public function forceDestroy(int $id): JsonResponse
    {
        $this->projectService->forceDeleteProject($id);

        return response()->json([
            'success' => true,
            'message' => '프로젝트가 영구 삭제되었습니다.',
        ]);
    }

    /**
     * 프로젝트 상태 변경
     */
    public function changeStatus(Request $request, int $id): JsonResponse
    {
        $validated = $request->validate([
            'status' => 'required|in:active,completed,on_hold',
        ]);

        $project = $this->projectService->changeStatus($id, $validated['status']);

        return response()->json([
            'success' => true,
            'message' => '프로젝트 상태가 변경되었습니다.',
            'data' => $project,
        ]);
    }

    /**
     * 프로젝트 복제
     */
    public function duplicate(Request $request, int $id): JsonResponse
    {
        $validated = $request->validate([
            'name' => 'nullable|string|max:100',
        ]);

        $project = $this->projectService->duplicateProject($id, $validated['name'] ?? null);

        return response()->json([
            'success' => true,
            'message' => '프로젝트가 복제되었습니다.',
            'data' => $project,
        ]);
    }
}
