<?php

namespace App\Http\Controllers\Api\V1;

use App\Http\Controllers\Controller;
use App\Http\Requests\Checklist\StoreEntryRequest;
use App\Http\Requests\Checklist\UpdateEntryRequest;
use App\Models\Unit;
use App\Services\ChecklistEntryService;
use App\Traits\ApiResponse;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;

class ChecklistEntryController extends Controller
{
    use ApiResponse;

    public function __construct(private readonly ChecklistEntryService $entryService) {}

    public function index(Request $request, int $unitId): JsonResponse
    {
        $unit    = Unit::findOrFail($unitId);
        $perPage = (int) $request->query('per_page', 15);
        $entries = $this->entryService->list($unit, $perPage);
        $entries->getCollection()->transform(fn ($entry) => $this->formatEntry($entry));

        return $this->success($entries, 'Checklist entries retrieved');
    }

    public function store(StoreEntryRequest $request, int $unitId): JsonResponse
    {
        $unit  = Unit::findOrFail($unitId);
        $entry = $this->entryService->create($unit, $request->validated() + ['image' => $request->file('image')], $request->user());

        return $this->created($this->formatEntry($entry), 'Checklist entry created');
    }

    public function show(int $unitId, int $entryId): JsonResponse
    {
        $unit  = Unit::findOrFail($unitId);
        $entry = $this->entryService->find($unit, $entryId);

        return $this->success($this->formatEntry($entry), 'Entry retrieved');
    }

    public function update(UpdateEntryRequest $request, int $unitId, int $entryId): JsonResponse
    {
        $unit  = Unit::findOrFail($unitId);
        $entry = $this->entryService->find($unit, $entryId);

        $updated = $this->entryService->update(
            $entry,
            $request->validated() + ['image' => $request->file('image')],
            $request->user()
        );

        return $this->success($this->formatEntry($updated), 'Entry updated successfully');
    }

    public function destroy(Request $request, int $unitId, int $entryId): JsonResponse
    {
        $unit  = Unit::findOrFail($unitId);
        $entry = $this->entryService->find($unit, $entryId);

        $this->entryService->delete($entry, $request->user());

        return $this->noContent('Entry deleted successfully');
    }

    private function formatEntry($entry): array
    {
        return [
            'id'                   => $entry->id,
            'unit_id'              => $entry->unit_id,
            'entered_by'           => $entry->enteredBy?->only(['id', 'name']),
            'entry_date'           => $entry->entry_date?->toDateString(),
            'entry_time'           => $entry->entry_time,
            'condition_status'     => $entry->condition_status,
            'issue_found'          => $entry->issue_found,
            'maintenance_required' => $entry->maintenance_required,
            'custom_fields'        => $entry->custom_fields,
            'notes'                => $entry->notes,
            'image_url'            => $entry->image_url,
            'last_edited_by'       => $entry->lastEditedBy?->only(['id', 'name']),
            'last_edited_at'       => $entry->last_edited_at,
            'created_at'           => $entry->created_at,
            'updated_at'           => $entry->updated_at,
        ];
    }
}
