<?php

namespace App\Http\Controllers\Api\V1;

use App\Actions\Lead\CreateLeadAction;
use App\DTOs\LeadData;
use App\Http\Controllers\Controller;
use App\Http\Requests\Lead\StoreLeadRequest;
use Illuminate\Http\JsonResponse;
use OpenApi\Attributes as OA;

class LeadController extends Controller
{
    #[OA\Post(
        path: '/api/v1/leads',
        summary: 'Yeni başvuru/talep oluştur',
        tags: ['Leads'],
        requestBody: new OA\RequestBody(
            required: true,
            content: new OA\JsonContent(
                required: ['name', 'phone', 'source', 'kvkk_consent'],
                properties: [
                    new OA\Property(property: 'name', type: 'string', description: 'Ad Soyad'),
                    new OA\Property(property: 'phone', type: 'string', description: 'Telefon'),
                    new OA\Property(property: 'email', type: 'string', format: 'email', nullable: true),
                    new OA\Property(property: 'source', type: 'string', description: 'kurs_kayit, danismanlik, duyuru, iletisim'),
                    new OA\Property(property: 'target_course', type: 'string', nullable: true, description: 'Kurs slug'),
                    new OA\Property(property: 'education_level', type: 'string', nullable: true),
                    new OA\Property(property: 'subject', type: 'string', nullable: true),
                    new OA\Property(property: 'message', type: 'string', nullable: true),
                    new OA\Property(property: 'kvkk_consent', type: 'boolean', description: 'KVKK onayı (zorunlu, true olmalı)'),
                    new OA\Property(property: 'marketing_consent', type: 'boolean', nullable: true),
                    new OA\Property(property: 'utm_source', type: 'string', nullable: true),
                    new OA\Property(property: 'utm_medium', type: 'string', nullable: true),
                    new OA\Property(property: 'utm_campaign', type: 'string', nullable: true),
                ],
            ),
        ),
        responses: [
            new OA\Response(response: 201, description: 'Başvuru alındı'),
            new OA\Response(response: 422, description: 'Validasyon hatası'),
        ],
    )]
    public function store(StoreLeadRequest $request, CreateLeadAction $action): JsonResponse
    {
        $dto = LeadData::fromArray($request->validated());
        $action->execute($dto);

        return response()->json([
            'success' => true,
            'message' => 'Talebiniz alınmıştır. En kısa sürede sizinle iletişime geçeceğiz.',
        ], 201);
    }
}
