<?php

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

use App\Http\Controllers\Controller;
use App\Http\Requests\Roadmap\StoreMilestoneRequest;
use App\Http\Requests\Roadmap\UpdateMilestoneRequest;
use App\Services\Roadmap\RoadmapMilestoneService;
use Illuminate\Http\JsonResponse;

class RoadmapMilestoneController extends Controller
{
    public function __construct(
        private readonly RoadmapMilestoneService $milestoneService
    ) {}

    public function byPlan(int $planId): JsonResponse
    {
        $milestones = $this->milestoneService->getMilestonesByPlan($planId);

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

    public function store(StoreMilestoneRequest $request): JsonResponse
    {
        $milestone = $this->milestoneService->createMilestone($request->validated());

        return response()->json([
            'success' => true,
            'message' => '마일스톤이 추가되었습니다.',
            'data' => $milestone,
        ]);
    }

    public function update(UpdateMilestoneRequest $request, int $id): JsonResponse
    {
        $this->milestoneService->updateMilestone($id, $request->validated());

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

    public function destroy(int $id): JsonResponse
    {
        $this->milestoneService->deleteMilestone($id);

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

    public function toggle(int $id): JsonResponse
    {
        $milestone = $this->milestoneService->toggleStatus($id);

        return response()->json([
            'success' => true,
            'message' => $milestone->status === 'completed' ? '마일스톤이 완료 처리되었습니다.' : '마일스톤이 미완료로 변경되었습니다.',
            'data' => $milestone,
        ]);
    }
}
