<?php

namespace Aura\Forms\Resources;

use Aura\Base\Resource;

class Form extends Resource
{
    public array $bulkActions = [
        'delete' => 'Löschen',
    ];
    // public static $customTable = true;

    public static ?string $slug = 'form';

    public static ?int $sort = 2;

    public static string $type = 'Form';

    protected $casts = [
        'options' => 'json',
        'data' => 'json',
    ];

    protected static $dropdown = 'Forms';

    protected static ?string $group = 'Admin';

    public static function getFields()
    {
        return [
            [
                'name' => 'Fields',
                'type' => 'Aura\\Base\\Fields\\Tab',
                'validation' => '',
                'global' => true,
                'conditional_logic' => [],
                'slug' => 'tab-fields',
            ],
            [
                'name' => 'Name',
                'type' => 'Aura\\Base\\Fields\\Text',
                'validation' => 'required',
                'searchable' => true,
                'on_index' => true,
                'slug' => 'title',
                'style' => [
                    'width' => '100',
                ],
            ],
            [
                'name' => 'Questions',
                'type' => 'Aura\\Base\\Fields\\Json',
                'on_index' => false,
                'on_forms' => false,
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'questions',
                'language' => 'json',
            ],
            [
                'name' => 'Questions',
                'type' => 'Aura\\Base\\Fields\\LivewireComponent',
                'component' => 'forms::questions-field',
                'on_index' => false,
                'on_create' => false,
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'questions',
                'language' => 'json',
            ],
            [
                'name' => 'Enabled',
                'type' => 'Aura\\Base\\Fields\\Boolean',
                'validation' => 'required',
                'conditional_logic' => [],
                'slug' => 'enabled',
            ],

            [
                'name' => 'Actions',
                'type' => 'Aura\\Base\\Fields\\Tab',
                'validation' => '',
                'global' => true,
                'conditional_logic' => function ($model) {
                    return false;
                },
                'slug' => 'tab-actions',
            ],

            // [
            //     'name' => 'Actions',
            //     'type' => 'Aura\\Base\\Fields\\LivewireComponent',
            //     'component' => 'forms::actions-field',
            //     'on_index' => false,
            //     'validation' => '',
            //     'conditional_logic' => [],
            //     'slug' => 'actions',
            //     'language' => 'json',
            // ],

            [
                'name' => 'Success Actions',
                'type' => 'Aura\\Base\\Fields\\Panel',
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'success_actions',
            ],
            [
                'name' => 'Action Type',
                'type' => 'Aura\\Base\\Fields\\Select',
                'validation' => '',
                'conditional_logic' => [],
                'on_index' => false,
                'slug' => 'success_action_type',
                'options' => [
                    'redirect' => 'Redirect',
                    'message' => 'Show message',
                    'none' => 'None',
                ],
            ],
            [
                'name' => 'Success message',
                'type' => 'Aura\\Base\\Fields\\Text',
                'validation' => '',
                'on_index' => false,
                'conditional_logic' => [
                    // [
                    //     'field' => 'success_action_type',
                    //     'operator' => '==',
                    //     'value' => 'message',
                    // ],
                ],
                'slug' => 'success_message',
            ],
            [
                'name' => 'Redirect URL',
                'on_index' => false,
                'type' => 'Aura\\Base\\Fields\\Text',
                'validation' => '',
                'conditional_logic' => [
                    // [
                    //     'field' => 'success_action_type',
                    //     'operator' => '==',
                    //     'value' => 'redirect',
                    // ],
                ],
                'slug' => 'success_redirect_url',
            ],
            [
                'name' => 'Empty Form after Submission',
                'on_index' => false,
                'type' => 'Aura\\Base\\Fields\\Boolean',
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'empty_form',
            ],
            [
                'name' => 'Hide Form after Submission',
                'type' => 'Aura\\Base\\Fields\\Boolean',
                'on_index' => false,
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'hide_form',
            ],

            [
                'name' => 'Email Notifications',
                'type' => 'Aura\\Base\\Fields\\Panel',
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'email_notifications',
            ],
            [
                'name' => 'Enable Email Notification',
                'type' => 'Aura\\Base\\Fields\\Boolean',
                'on_index' => false,
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'enable_email_notification',
            ],
            [
                'name' => 'Recipient Email(s)',
                'type' => 'Aura\\Base\\Fields\\Text',
                'on_index' => false,
                'validation' => '',
                'conditional_logic' => [
                    // [
                    //     'field' => 'enable_email_notification',
                    //     'operator' => '==',
                    //     'value' => '1',
                    // ],
                ],
                'slug' => 'recipient_emails',
            ],
            [
                'name' => 'Email Subject',
                'type' => 'Aura\\Base\\Fields\\Text',
                'on_index' => false,
                'validation' => '',
                'conditional_logic' => [
                    // [
                    //     'field' => 'enable_email_notification',
                    //     'operator' => '==',
                    //     'value' => '1',
                    // ],
                ],
                'slug' => 'email_subject',
            ],
            [
                'name' => 'Email Body',
                'type' => 'Aura\\Base\\Fields\\Textarea',
                'on_index' => false,
                'validation' => '',
                'conditional_logic' => [
                    // [
                    //     'field' => 'enable_email_notification',
                    //     'operator' => '==',
                    //     'value' => '1',
                    // ],
                ],
                'slug' => 'email_body',
            ],

            [
                'name' => 'Mailing List Integration',
                'type' => 'Aura\\Base\\Fields\\Panel',
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'mailing_list_integration',
            ],
            [
                'name' => 'Enable Mailing List',
                'type' => 'Aura\\Base\\Fields\\Boolean',
                'on_index' => false,
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'enable_mailing_list',
            ],
            [
                'name' => 'Mailing List Service',
                'type' => 'Aura\\Base\\Fields\\Select',
                'on_index' => false,
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'mailing_list_service',
                'options' => [
                    // Add your mailing list services here, e.g., 'mailchimp' => 'Mailchimp'
                    'mailchimp' => 'Mailchimp',
                ],
            ],
            [
                'name' => 'API Key',
                'type' => 'Aura\\Base\\Fields\\Text',
                'on_index' => false,
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'api_key',
            ],
            [
                'name' => 'List ID',
                'type' => 'Aura\\Base\\Fields\\Text',
                'on_index' => false,
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'list_id',
            ],

            [
                'name' => 'Submission Restrictions',
                'type' => 'Aura\Base\Fields\Panel',
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'submission_restrictions',
                'instructions' => 'Toggle to allow only one submission per browser using local storage.',
            ],
            [
                'name' => 'Enable One-time Submission',
                'type' => 'Aura\Base\Fields\Boolean',
                'on_index' => false,
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'enable_one_time_submission',
                'instructions' => 'Toggle to allow only one submission per browser using local storage.',
            ],
            [
                'name' => 'Submission Limit',
                'type' => 'Aura\Base\Fields\Number',
                'on_index' => false,
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'submission_limit',
                'instructions' => 'Set the number of submissions allowed for this form.',
            ],
            [
                'name' => 'Start Date',
                'type' => 'Aura\Base\Fields\Date',
                'on_index' => false,
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'start_date',
                'instructions' => 'The date when the form will start accepting submissions.',
            ],
            [
                'name' => 'End Date',
                'type' => 'Aura\Base\Fields\Date',
                'on_index' => false,
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'end_date',
                'instructions' => 'The date when the form will stop accepting submissions.',
            ],
            [
                'name' => 'Submission Interval',
                'type' => 'Aura\Base\Fields\Number',
                'on_index' => false,
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'submission_interval',
                'instructions' => 'The minimum number of minutes required between two consecutive submissions from the same user (0 for no restriction).',
            ],

            // [
            //     'name' => 'Submissions',
            //     'type' => 'Aura\\Base\\Fields\\Tab',
            //     'validation' => '',
            //     'global' => true,
            //     'conditional_logic' => [],
            //     'slug' => 'tab-submissions',
            // ],
            // [
            //     'name' => 'Submissions',
            //     'slug' => 'submissions',
            //     'type' => 'Aura\\Base\\Fields\\HasMany',
            //     'resource' => 'Aura\\Forms\\Resources\\Submission',
            //     'relation' => 'form_id',
            //     'validation' => '',
            //     'conditional_logic' => [],
            //     'wrapper' => '',
            //     'on_index' => false,
            //     'on_forms' => true,
            //     'on_view' => true,
            //     'style' => [
            //         'width' => '100',
            //     ],
            // ],
        ];
    }

    public function getIcon()
    {
        return '<svg class="w-5 h-5" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M14 11H8M10 15H8M16 7H8M20 10.5V6.8C20 5.11984 20 4.27976 19.673 3.63803C19.3854 3.07354 18.9265 2.6146 18.362 2.32698C17.7202 2 16.8802 2 15.2 2H8.8C7.11984 2 6.27976 2 5.63803 2.32698C5.07354 2.6146 4.6146 3.07354 4.32698 3.63803C4 4.27976 4 5.11984 4 6.8V17.2C4 18.8802 4 19.7202 4.32698 20.362C4.6146 20.9265 5.07354 21.3854 5.63803 21.673C6.27976 22 7.11984 22 8.8 22H11.5M22 22L20.5 20.5M21.5 18C21.5 19.933 19.933 21.5 18 21.5C16.067 21.5 14.5 19.933 14.5 18C14.5 16.067 16.067 14.5 18 14.5C19.933 14.5 21.5 16.067 21.5 18Z" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round"/></svg>';
    }

    public static function getWidgets(): array
    {
        return [];
    }

    // save function
    public function save(array $options = [])
    {
        // dd($this->questions);

        parent::save($options);
    }

    public function submissions()
    {
        return Submission::whereHas('meta', function ($query) {
            $query->where('key', 'form_id')->where('value', $this->id);
        })->get();
    }

    public function team()
    {
        return $this->belongsTo(Team::class);
    }

    // protected $table = 'forms';

    public function title()
    {
        return optional($this)->title." (Formular #{$this->id})";
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}
