<?php

namespace App\Http\Controllers;

use App\Models\Project;
use App\Models\ProjectTask;
use Illuminate\Http\Request;
use Illuminate\Validation\Rule;

class ProjectTaskController extends Controller
{
    public function index(Request $request, $projectId)
    {
        $project = Project::find($projectId);

        if (!$project) {
            return response()->json(['message' => 'Project not found'], 404);
        }

        $query = ProjectTask::query()
            ->with(['assignedTo:id,name,email,role', 'createdBy:id,name,email,role'])
            ->where('project_id', $projectId)
            ->latest();

        if ($request->filled('status')) {
            $query->where('status', $request->status);
        }

        $tasks = $query->get();

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

    public function store(Request $request, $projectId)
    {
        $request->validate([
            'title' => 'required|string|max:255',
            'description' => 'nullable|string',
            'status' => ['nullable', Rule::in(['pending', 'in_progress', 'completed'])],
            'due_date' => 'nullable|date',
            'assigned_to_id' => 'nullable|exists:users,id',
        ]);

        $project = Project::find($projectId);
        if (!$project) {
            return response()->json(['message' => 'Project not found'], 404);
        }

        $task = ProjectTask::create([
            'project_id' => $project->id,
            'title' => $request->title,
            'description' => $request->description,
            'status' => $request->status ?? 'pending',
            'due_date' => $request->due_date,
            'assigned_to_id' => $request->assigned_to_id,
            'created_by_id' => auth('api')->id(),
        ]);

        $task->load(['assignedTo:id,name,email,role', 'createdBy:id,name,email,role']);

        return response()->json([
            'data' => $task,
        ], 201);
    }

    public function update(Request $request, $projectId, $taskId)
    {
        $request->validate([
            'title' => 'nullable|string|max:255',
            'description' => 'nullable|string',
            'status' => ['nullable', Rule::in(['pending', 'in_progress', 'completed'])],
            'due_date' => 'nullable|date',
            'assigned_to_id' => 'nullable|exists:users,id',
        ]);

        $task = ProjectTask::where('project_id', $projectId)->find($taskId);
        if (!$task) {
            return response()->json(['message' => 'Task not found'], 404);
        }

        $task->update($request->only([
            'title',
            'description',
            'status',
            'due_date',
            'assigned_to_id',
        ]));

        $task->load(['assignedTo:id,name,email,role', 'createdBy:id,name,email,role']);

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

    public function destroy($projectId, $taskId)
    {
        $task = ProjectTask::where('project_id', $projectId)->find($taskId);
        if (!$task) {
            return response()->json(['message' => 'Task not found'], 404);
        }

        $task->delete();

        return response()->json([
            'message' => 'Task deleted successfully',
        ]);
    }
}
