<?php

namespace Aura\Forms\Resources;

use Eminiarts\Aura\Resource;

class Form extends Resource
{
    // 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 = 'Aura';

    // protected $table = 'forms';


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

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

            [
                'name' => 'Actions',
                'type' => 'Eminiarts\\Aura\\Fields\\Tab',
                'validation' => '',
                'global' => true,
                'conditional_logic' => [],
                'slug' => 'tab-actions',
            ],

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

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

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

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

            [
                'name' => 'Submission Restrictions',
                'type' => 'Eminiarts\Aura\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' => 'Eminiarts\Aura\Fields\Boolean',
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'enable_one_time_submission',
                'instructions' => 'Toggle to allow only one submission per browser using local storage.',
            ],
            [
                'name' => 'Submission Limit',
                'type' => 'Eminiarts\Aura\Fields\Number',
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'submission_limit',
                'instructions' => 'Set the number of submissions allowed for this form.',
            ],
            [
                'name' => 'Start Date',
                'type' => 'Eminiarts\Aura\Fields\Date',
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'start_date',
                'instructions' => 'The date when the form will start accepting submissions.',
            ],
            [
                'name' => 'End Date',
                'type' => 'Eminiarts\Aura\Fields\Date',
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'end_date',
                'instructions' => 'The date when the form will stop accepting submissions.',
            ],
            [
                'name' => 'Submission Interval',
                'type' => 'Eminiarts\Aura\Fields\Number',
                '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' => 'Eminiarts\\Aura\\Fields\\Tab',
                'validation' => '',
                'global' => true,
                'conditional_logic' => [],
                'slug' => 'tab-submissions',
            ],
            [
                'name' => 'Submissions',
                'slug' => 'submissions',
                'type' => 'Eminiarts\\Aura\\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 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);
    }

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