<?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',
    ];

    public array $bulkActions = [
        'delete' => 'Löschen',
    ];

    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>';
    }

    protected static $dropdown = 'Forms';

    protected static ?string $group = 'Admin';

    // 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' => function ($model) {
                    ray(auth()->user()->email);
                    if(auth()->user()->email == 'bajram@eminiarts.ch') {
                        return false;
                    }
                    return false;
                },
                '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' => [],
                'on_index' => false,
                'slug' => 'success_action_type',
                'options' => [
                    'redirect' => 'Redirect',
                    'message' => 'Show message',
                    'none' => 'None',
                ],
            ],
            [
                'name' => 'Success message',
                'type' => 'Eminiarts\\Aura\\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' => 'Eminiarts\\Aura\\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' => 'Eminiarts\\Aura\\Fields\\Boolean',
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'empty_form',
            ],
            [
                'name' => 'Hide Form after Submission',
                'type' => 'Eminiarts\\Aura\\Fields\\Boolean',
                'on_index' => false,
                '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',
                'on_index' => false,
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'enable_email_notification',
            ],
            [
                'name' => 'Recipient Email(s)',
                'type' => 'Eminiarts\\Aura\\Fields\\Text',
                'on_index' => false,
                'validation' => '',
                'conditional_logic' => [
                    // [
                    //     'field' => 'enable_email_notification',
                    //     'operator' => '==',
                    //     'value' => '1',
                    // ],
                ],
                'slug' => 'recipient_emails',
            ],
            [
                'name' => 'Email Subject',
                'type' => 'Eminiarts\\Aura\\Fields\\Text',
                'on_index' => false,
                'validation' => '',
                'conditional_logic' => [
                    // [
                    //     'field' => 'enable_email_notification',
                    //     'operator' => '==',
                    //     'value' => '1',
                    // ],
                ],
                'slug' => 'email_subject',
            ],
            [
                'name' => 'Email Body',
                'type' => 'Eminiarts\\Aura\\Fields\\Textarea',
                'on_index' => false,
                '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',
                'on_index' => false,
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'enable_mailing_list',
            ],
            [
                'name' => 'Mailing List Service',
                'type' => 'Eminiarts\\Aura\\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' => 'Eminiarts\\Aura\\Fields\\Text',
                'on_index' => false,
                'validation' => '',
                'conditional_logic' => [],
                'slug' => 'api_key',
            ],
            [
                'name' => 'List ID',
                'type' => 'Eminiarts\\Aura\\Fields\\Text',
                'on_index' => false,
                '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',
                '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' => 'Eminiarts\Aura\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' => 'Eminiarts\Aura\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' => 'Eminiarts\Aura\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' => 'Eminiarts\Aura\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' => '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);
    }
}
