<?php

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

use App\Http\Controllers\Controller;
use App\Http\Requests\Roadmap\StorePlanRequest;
use App\Http\Requests\Roadmap\UpdatePlanRequest;
use App\Services\Roadmap\RoadmapPlanService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\View\View;

class RoadmapPlanController extends Controller
{
    public function __construct(
        private readonly RoadmapPlanService $planService
    ) {}

    public function index(Request $request): View|JsonResponse
    {
        $filters = $request->only([
            'search', 'status', 'category', 'priority', 'phase',
            'trashed', 'sort_by', 'sort_direction',
        ]);
        $plans = $this->planService->getPlans($filters, 15);

        if ($request->header('HX-Request')) {
            return view('roadmap.plans.partials.table', compact('plans'));
        }

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

    public function stats(): JsonResponse
    {
        $stats = $this->planService->getStats();

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

    public function timeline(Request $request): JsonResponse
    {
        $phase = $request->input('phase');
        $timeline = $this->planService->getTimelineData($phase);

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

    public function show(int $id): JsonResponse
    {
        $plan = $this->planService->getPlanById($id, true);

        if (! $plan) {
            return response()->json([
                'success' => false,
                'message' => '계획을 찾을 수 없습니다.',
            ], 404);
        }

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

    public function store(StorePlanRequest $request): JsonResponse
    {
        $plan = $this->planService->createPlan($request->validated());

        return response()->json([
            'success' => true,
            'message' => '계획이 생성되었습니다.',
            'data' => $plan,
        ]);
    }

    public function update(UpdatePlanRequest $request, int $id): JsonResponse
    {
        $this->planService->updatePlan($id, $request->validated());

        return response()->json([
            'success' => true,
            'message' => '계획이 수정되었습니다.',
        ]);
    }

    public function destroy(int $id): JsonResponse
    {
        $this->planService->deletePlan($id);

        return response()->json([
            'success' => true,
            'message' => '계획이 삭제되었습니다.',
        ]);
    }

    public function restore(int $id): JsonResponse
    {
        $this->planService->restorePlan($id);

        return response()->json([
            'success' => true,
            'message' => '계획이 복원되었습니다.',
        ]);
    }

    public function changeStatus(Request $request, int $id): JsonResponse
    {
        $validated = $request->validate([
            'status' => 'required|in:planned,in_progress,completed,delayed,cancelled',
        ]);

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

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