<?php
declare(strict_types=1);

use Celena\Core\Database\Connection;
use Celena\Core\Database\Schema;

/**
 * Сид дефолтной формы «contact» — базовая форма заявки. Дальше редактируется
 * в конструкторе (/admin/forms): поля, тексты, получатели, Telegram. Идемпотентно.
 */
return function (Schema $schema, Connection $conn): void {
    $exists = $conn->builder('forms')->where('slug', '=', 'contact')->first();
    if ($exists !== null) {
        return;
    }

    $fields = [
        ['key' => 'name',    'label' => 'Your name',          'type' => 'text',     'required' => true,  'width' => 'half'],
        ['key' => 'phone',   'label' => 'Phone / WhatsApp',   'type' => 'tel',      'required' => true,  'width' => 'half'],
        ['key' => 'email',   'label' => 'Email',              'type' => 'email',    'required' => false, 'width' => 'half', 'placeholder' => 'you@example.com'],
        ['key' => 'service', 'label' => 'What do you need?',  'type' => 'select',   'required' => false, 'width' => 'half',
            'options' => [
                ['value' => 'Business website'],
                ['value' => 'Online store'],
                ['value' => 'Web app / CRM'],
                ['value' => 'Mobile app'],
                ['value' => 'SEO / Advertising'],
                ['value' => 'Logo / Branding'],
                ['value' => 'Other'],
            ]],
        ['key' => 'message', 'label' => 'Project details',    'type' => 'textarea', 'required' => false, 'width' => 'full'],
    ];

    $settings = [
        'is_default'         => true,
        'endpoint'           => '/api/leads/submit',
        'track_event'        => 'generate_lead',
        // Конверсия Google Ads: свой send_to задаётся в настройках формы.
        'conversion_send_to' => '',
        'lead_value'         => 0,
        'lead_currency'      => 'EUR',
        'title'              => 'Request a quote',
        'submit_text'        => 'Send request',
        'sending_text'       => 'Sending...',
        'ok_title'           => 'Thank you!',
        'ok_text'            => 'We have received your request and will get back to you shortly.',
        'note'               => '',
        // Получатели писем — задаются в настройках формы. Пусто = заявка только в админке.
        'notify_emails'      => [],
        'telegram_enabled'   => false,
    ];

    $now = date('Y-m-d H:i:s');
    $conn->builder('forms')->insert([
        'slug'       => 'contact',
        'title'      => 'Contact form',
        'fields'     => json_encode($fields, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
        'settings'   => json_encode($settings, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES),
        'status'     => 'active',
        'created_at' => $now,
        'updated_at' => $now,
    ]);
};
