<?php

namespace App\Http\Controllers;

use App\Models\SiteInspection;
use Illuminate\Http\Request;

class SiteInspectionController extends Controller
{
    public function index(Request $request)
    {
        $query = SiteInspection::query()->with(['project', 'creator']);

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

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

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

        $inspections = $query->latest('scheduled_at')->get();

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

    public function store(Request $request)
    {
        $request->validate([
            'project_id' => 'required|exists:projects,id',
            'type' => 'required|in:inspection,meeting',
            'title' => 'required|string|max:255',
            'description' => 'nullable|string',
            'scheduled_at' => 'required|date',
            'attendees' => 'nullable|array',
            'notes' => 'nullable|string',
        ]);

        $inspection = SiteInspection::create([
            'project_id' => $request->project_id,
            'type' => $request->type,
            'title' => $request->title,
            'description' => $request->description,
            'scheduled_at' => $request->scheduled_at,
            'status' => 'scheduled',
            'attendees' => $request->attendees,
            'notes' => $request->notes,
            'created_by' => auth('api')->id(),
        ]);

        $inspection->load(['project', 'creator']);

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

    public function update(Request $request, $id)
    {
        $inspection = SiteInspection::find($id);

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

        $request->validate([
            'title' => 'sometimes|string|max:255',
            'description' => 'nullable|string',
            'scheduled_at' => 'sometimes|date',
            'status' => 'sometimes|in:scheduled,completed,cancelled',
            'attendees' => 'nullable|array',
            'notes' => 'nullable|string',
        ]);

        $inspection->update($request->only([
            'title',
            'description',
            'scheduled_at',
            'status',
            'attendees',
            'notes',
        ]));

        $inspection->load(['project', 'creator']);

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

    public function destroy($id)
    {
        $inspection = SiteInspection::find($id);

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

        $inspection->delete();

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