<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Http\Resources\ChecklistResource;
use App\Models\Checklist;
use App\Models\Task;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ChecklistController extends Controller
{
    public function index(Request $request, Task $task): JsonResponse
    {
        try {
            $checklists = $task->checklists()->orderBy('position')->get();

            return response()->json([
                'success' => true,
                'data' => ChecklistResource::collection($checklists),
                'message' => 'لیست چک‌لیست‌ها',
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'خطا در دریافت چک‌لیست‌ها',
            ], 500);
        }
    }

    public function store(Request $request, Task $task): JsonResponse
    {
        try {
            $request->validate([
                'title' => 'required|string|max:255',
                'position' => 'nullable|integer',
            ]);

            $data = $request->only(['title', 'position']);
            $data['task_id'] = $task->id;
            $data['is_completed'] = false;
            $checklist = Checklist::create($data);

            return response()->json([
                'success' => true,
                'data' => new ChecklistResource($checklist),
                'message' => 'آیتم چک‌لیست ایجاد شد',
            ], 201);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'خطا در ایجاد چک‌لیست',
            ], 500);
        }
    }

    public function update(Request $request, Checklist $checklist): JsonResponse
    {
        try {
            $request->validate([
                'title' => 'sometimes|required|string|max:255',
                'position' => 'nullable|integer',
            ]);

            $checklist->update($request->only(['title', 'position']));

            return response()->json([
                'success' => true,
                'data' => new ChecklistResource($checklist->fresh()),
                'message' => 'چک‌لیست به‌روزرسانی شد',
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'خطا در به‌روزرسانی چک‌لیست',
            ], 500);
        }
    }

    public function destroy(Checklist $checklist): JsonResponse
    {
        try {
            $checklist->delete();

            return response()->json([
                'success' => true,
                'message' => 'چک‌لیست حذف شد',
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'خطا در حذف چک‌لیست',
            ], 500);
        }
    }

    public function toggleComplete(Checklist $checklist): JsonResponse
    {
        try {
            $checklist->update(['is_completed' => ! $checklist->is_completed]);

            return response()->json([
                'success' => true,
                'data' => new ChecklistResource($checklist->fresh()),
                'message' => $checklist->is_completed ? 'تکمیل شد' : 'ناتمام',
            ]);
        } catch (\Exception $e) {
            return response()->json([
                'success' => false,
                'message' => 'خطا در تغییر وضعیت چک‌لیست',
            ], 500);
        }
    }
}
