<?php
require_once __DIR__ . '/../../public/vendor/autoload.php';
use App\Models\DynamicForm;
use App\Models\DynamicFormField;

// use model instances (they provide DB connection internally)
$formModel = new DynamicForm();
$fieldModel = new DynamicFormField();

// using model methods for DB access

$templates = [
    [
        'title' => 'Contact Form',
        'description' => 'Basic contact form',
        'fields' => [
            ['label' => 'Full Name', 'field_type' => 'text', 'placeholder' => 'Full name', 'is_required' => 1],
            ['label' => 'Email', 'field_type' => 'email', 'placeholder' => 'Email address', 'is_required' => 1],
            ['label' => 'Phone', 'field_type' => 'phone', 'placeholder' => 'Phone number', 'is_required' => 0],
            ['label' => 'Message', 'field_type' => 'textarea', 'placeholder' => 'Your message', 'is_required' => 1],
        ]
    ],
    [
        'title' => 'Event Registration',
        'description' => 'Event signup form',
        'fields' => [
            ['label' => 'Full Name', 'field_type' => 'text', 'placeholder' => 'Full name', 'is_required' => 1],
            ['label' => 'Email', 'field_type' => 'email', 'placeholder' => 'Email address', 'is_required' => 1],
            ['label' => 'Ticket Type', 'field_type' => 'select', 'options' => 'General, VIP, Student', 'is_required' => 1],
            ['label' => 'Dietary Preferences', 'field_type' => 'radio', 'options' => 'None, Vegetarian, Vegan, Halal', 'is_required' => 0],
        ]
    ],
    [
        'title' => 'Feedback Survey',
        'description' => 'Simple feedback form',
        'fields' => [
            ['label' => 'Name', 'field_type' => 'text', 'placeholder' => '', 'is_required' => 0],
            ['label' => 'Rating', 'field_type' => 'radio', 'options' => '1,2,3,4,5', 'is_required' => 1],
            ['label' => 'Comments', 'field_type' => 'textarea', 'placeholder' => 'Any comments?', 'is_required' => 0],
        ]
    ],
];

foreach ($templates as $tpl) {
    // Skip if template with same title exists
    $exists = $formModel->raw("SELECT id FROM dynamic_forms WHERE title = ? LIMIT 1", [$tpl['title']]);
    if (!empty($exists)) {
        echo "Template '{$tpl['title']}' already exists, skipping.\n";
        continue;
    }

    $newId = $formModel->create([
        'title' => $tpl['title'],
        'description' => $tpl['description'],
        'table_name' => 'template_' . time() . rand(100,999),
        'is_active' => 0,
        'requires_auth' => 0,
        'is_template' => 1,
        'template_name' => $tpl['title'],
        'created_by' => 1,
    ]);

    foreach ($tpl['fields'] as $i => $f) {
        $fieldModel->create([
            'form_id' => $newId,
            'label' => $f['label'],
            'field_name' => 'field_' . ($i + 1),
            'field_type' => $f['field_type'],
            'options' => $f['options'] ?? null,
            'is_required' => $f['is_required'] ?? 0,
            'placeholder' => $f['placeholder'] ?? null,
            'sort_order' => $i + 1,
        ]);
    }
    echo "Created template: {$tpl['title']} (id={$newId})\n";
}
