<?php

namespace App\Http\Controllers\Api\Admin;

use App\Actions\Lead\DeleteLeadAction;
use App\Actions\Lead\UpdateLeadAction;
use App\DTOs\LeadData;
use App\Http\Controllers\Controller;
use App\Http\Requests\Lead\UpdateLeadRequest;
use App\Http\Resources\LeadResource;
use App\Models\Lead;
use App\Repositories\Contracts\LeadRepositoryInterface;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\AnonymousResourceCollection;
use OpenApi\Attributes as OA;

class LeadController extends Controller
{
    public function __construct(private LeadRepositoryInterface $repository) {}

    #[OA\Get(
        path: '/api/admin/leads',
        summary: 'Başvuruları listele (Admin)',
        tags: ['Admin - Leads'],
        security: [['sanctum' => []]],
        parameters: [
            new OA\Parameter(name: 'status', in: 'query', required: false, schema: new OA\Schema(type: 'string')),
            new OA\Parameter(name: 'source', in: 'query', required: false, schema: new OA\Schema(type: 'string')),
            new OA\Parameter(name: 'is_read', in: 'query', required: false, schema: new OA\Schema(type: 'boolean')),
            new OA\Parameter(name: 'search', in: 'query', required: false, schema: new OA\Schema(type: 'string')),
            new OA\Parameter(name: 'per_page', in: 'query', required: false, schema: new OA\Schema(type: 'integer', default: 15)),
        ],
        responses: [new OA\Response(response: 200, description: 'Başvuru listesi')],
    )]
    public function index(Request $request): AnonymousResourceCollection
    {
        $leads = $this->repository->paginate(
            $request->only(['status', 'source', 'is_read', 'search']),
            $request->integer('per_page', 15),
        );

        return LeadResource::collection($leads);
    }

    #[OA\Get(
        path: '/api/admin/leads/{lead}',
        summary: 'Başvuru detayı',
        tags: ['Admin - Leads'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'lead', in: 'path', required: true, schema: new OA\Schema(type: 'integer'))],
        responses: [new OA\Response(response: 200, description: 'Başvuru detayı')],
    )]
    public function show(Lead $lead): JsonResponse
    {
        if (! $lead->is_read) {
            $lead->update(['is_read' => true]);
        }

        return response()->json(new LeadResource($lead));
    }

    #[OA\Put(
        path: '/api/admin/leads/{lead}',
        summary: 'Başvuru güncelle',
        tags: ['Admin - Leads'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'lead', in: 'path', required: true, schema: new OA\Schema(type: 'integer'))],
        requestBody: new OA\RequestBody(required: true, content: new OA\JsonContent(
            properties: [
                new OA\Property(property: 'status', type: 'string'),
                new OA\Property(property: 'is_read', type: 'boolean'),
                new OA\Property(property: 'admin_notes', type: 'string'),
            ],
        )),
        responses: [
            new OA\Response(response: 200, description: 'Başvuru güncellendi'),
            new OA\Response(response: 422, description: 'Validasyon hatası'),
        ],
    )]
    public function update(UpdateLeadRequest $request, Lead $lead, UpdateLeadAction $action): JsonResponse
    {
        $dto = LeadData::fromArray(array_merge($lead->toArray(), $request->validated()));
        $lead = $action->execute($lead, $dto);

        return response()->json(new LeadResource($lead));
    }

    #[OA\Delete(
        path: '/api/admin/leads/{lead}',
        summary: 'Başvuru sil',
        tags: ['Admin - Leads'],
        security: [['sanctum' => []]],
        parameters: [new OA\Parameter(name: 'lead', in: 'path', required: true, schema: new OA\Schema(type: 'integer'))],
        responses: [new OA\Response(response: 200, description: 'Başvuru silindi')],
    )]
    public function destroy(Lead $lead, DeleteLeadAction $action): JsonResponse
    {
        $action->execute($lead);

        return response()->json(['message' => 'Talep silindi.']);
    }
}
