<?php

namespace App\DTOs;

class LeadData
{
    public function __construct(
        public readonly string $name,
        public readonly string $phone,
        public readonly string $source,
        public readonly ?string $email = null,
        public readonly ?string $targetCourse = null,
        public readonly ?string $educationLevel = null,
        public readonly ?string $subject = null,
        public readonly ?string $message = null,
        public readonly ?array $utm = null,
        public readonly bool $consentKvkk = false,
        public readonly bool $marketingConsent = false,
        public readonly ?string $consentTextVersion = null,
    ) {}

    /**
     * @param  array<string, mixed>  $data
     */
    public static function fromArray(array $data): self
    {
        $utm = array_filter([
            'utm_source' => $data['utm_source'] ?? null,
            'utm_medium' => $data['utm_medium'] ?? null,
            'utm_campaign' => $data['utm_campaign'] ?? null,
        ]);

        return new self(
            name: $data['name'],
            phone: $data['phone'],
            source: $data['source'],
            email: $data['email'] ?? null,
            targetCourse: $data['target_course'] ?? null,
            educationLevel: $data['education_level'] ?? null,
            subject: $data['subject'] ?? null,
            message: $data['message'] ?? null,
            utm: $utm ?: null,
            consentKvkk: (bool) ($data['kvkk_consent'] ?? false),
            marketingConsent: (bool) ($data['marketing_consent'] ?? false),
            consentTextVersion: $data['consent_text_version'] ?? null,
        );
    }

    /**
     * @return array<string, mixed>
     */
    public function toArray(): array
    {
        return [
            'name' => $this->name,
            'phone' => $this->phone,
            'email' => $this->email,
            'source' => $this->source,
            'target_course' => $this->targetCourse,
            'education_level' => $this->educationLevel,
            'subject' => $this->subject,
            'message' => $this->message,
            'utm' => $this->utm,
            'consent_kvkk' => $this->consentKvkk,
            'marketing_consent' => $this->marketingConsent,
            'consent_text_version' => $this->consentTextVersion,
        ];
    }
}
