<?php

namespace Storeplugin\PDFInvoice\App\Settings;

use Storeplugin\PDFInvoice\Framework\Contracts\ServiceInterface;
use Storeplugin\PDFInvoice\Framework\Forms\Setting;

class InvoiceSetting implements ServiceInterface
{
    /**
     * Register the settings panel.
     */
    public function register(): void
    {
        Setting::create(
            'Store Invoice',
            'Settings',
            'nxtpdf-invoice-settings',
            'nxtpdf_settings',
            'manage_options',
            'dashicons-admin-generic',
        )
        ->withAdminSubmenu('nxtpdf-invoice-editor-panel')
        ->addTab('general', 'General', 'admin-generic', function($tab) {
            $tab->tabLink('Company Information', 'Core site fields and performance options.')
                ->addPanel([
                    'title' => 'Company Details',
                    'icon' => 'list-view',
                    'fields' => [
                        [
                            'id' => 'company_name',
                            'label' => 'Company Name',
                            'type' => 'text',
                            'placeholder' => 'Enter company name',
                            'description' => 'Your business or company name.',
                            'tip' => 'Your business or company name.',
                        ],
                        [
                            'id' => 'logo_url',
                            'label' => 'Logo',
                            'type' => 'file_upload',
                            'placeholder' => 'No image selected',
                            'button_label' => 'Select image',
                            'remove_label' => 'Remove image',
                            'modal_title' => 'Choose a logo',
                            'modal_button' => 'Use this image',
                            'library_type' => 'image',
                            'allowed_extensions' => ['jpg', 'jpeg', 'png'],
                            'invalid_format_message' => 'Please select a JPG or PNG image.',
                            'description' => 'Upload your company logo.',
                        ],
                        [
                            'id' => 'company_address_1',
                            'label' => 'Address Line 1',
                            'type' => 'text',
                            'placeholder' => 'Enter company address line 1',
                            'description' => 'Street address of your company.',
                        ],
                        [
                            'id' => 'company_address_2',
                            'label' => 'Address Line 2',
                            'type' => 'text',
                            'placeholder' => 'Apartment, suite, unit, etc.',
                            'description' => 'Additional address information (optional).',
                        ],
                        [
                            'id' => 'company_city',
                            'label' => 'City',
                            'type' => 'text',
                            'placeholder' => 'City',
                        ],
                        [
                            'id' => 'company_state',
                            'label' => 'State / Province',
                            'type' => 'text',
                            'placeholder' => 'State or province',
                        ],
                        [
                            'id' => 'company_postcode',
                            'label' => 'Postcode / ZIP',
                            'type' => 'text',
                            'placeholder' => 'Postal code',
                        ],
                        [
                            'id' => 'company_country',
                            'label' => 'Country',
                            'type' => 'text',
                            'placeholder' => 'Country',
                        ],
                        [
                            'id' => 'company_phone',
                            'label' => 'Phone',
                            'type' => 'text',
                            'placeholder' => '+1 234 567 8900',
                            'description' => 'Business phone number.',
                        ],
                        [
                            'id' => 'company_email',
                            'label' => 'Email',
                            'type' => 'email',
                            'placeholder' => 'billing@company.com',
                            'description' => 'Business email address.',
                        ],
                        [
                            'id' => 'company_vat',
                            'label' => 'VAT Number',
                            'type' => 'text',
                            'placeholder' => 'VAT/Tax ID',
                            'description' => 'Value Added Tax identification number.',
                        ],
                        [
                            'id' => 'company_coc',
                            'label' => 'Chamber of Commerce',
                            'disabled' => true,
                            'type' => 'text',
                            'placeholder' => 'CoC Number',
                            'description' => 'Chamber of Commerce registration number.',
                            'tip' => 'This field is currently disabled in the free version.',
                        ],
                        [
                            'id' => 'company_website',
                            'label' => 'Website',
                            'type' => 'text',
                            'placeholder' => 'https://example.com',
                            'description' => 'Company website URL.',
                        ],
                        [
                            'id' => 'company_registration',
                            'label' => 'Registration Number',
                            'disabled' => true,
                            'type' => 'text',
                            'placeholder' => 'Company Reg. No.',
                            'description' => 'Business registration number.',
                            'tip' => 'This field is currently disabled in the free version.',
                        ],
                        [
                            'id' => 'company_gstin',
                            'label' => 'GSTIN',
                            'disabled' => true,
                            'type' => 'text',
                            'placeholder' => 'GST Identification Number',
                            'description' => 'GST Identification Number (India).',
                            'tip' => 'This field is currently disabled in the free version.',
                        ],
                        [
                            'id' => 'company_abn',
                            'label' => 'ABN',
                            'disabled' => true,
                            'type' => 'text',
                            'placeholder' => 'Australian Business Number',
                            'description' => 'Australian Business Number.',
                            'tip' => 'This field is currently disabled in the free version.',
                        ],
                        [
                            'id' => 'bank_details',
                            'label' => 'Bank Details',
                            'disabled' => true,
                            'type' => 'textarea',
                            'placeholder' => 'Bank name, Account number, etc.',
                            'description' => 'Bank account details for payment.',
                            'tip' => 'This field is currently disabled in the free version.',
                        ],
                        [
                            'id' => 'additional_header_text',
                            'label' => 'Additional Header Text',
                            'type' => 'textarea',
                            'placeholder' => 'Additional text to display in header',
                            'description' => 'Extra text shown in the company header area.',
                        ],

                    ]
                ]);

            $tab->tabLink('File & Output', 'Branding and tagline settings used across the site.')
                ->addPanel([
                    'title' => 'File & Output Settings',
                    'icon' => 'dashicons-media-default',
                    'fields' => [
                        [
                            'id' => 'pdf_filename_format',
                            'label' => 'PDF Filename Format',
                            'type' => 'text',
                            'placeholder' => 'invoice-{order_number}',
                            'description' => 'Placeholders: {order_number}, {invoice_number}, {date}, {Y}, {m}, {d}',
                        ],
                        [
                            'id' => 'apply_latest_settings',
                            'label' => 'Apply Latest Settings to Old Invoices',
                            'type' => 'switch',
                            'description' => 'Use current template settings when regenerating older invoices.',
                        ],
                    ]
                ]);

            $tab->tabLink('Email Automation', 'Configure automatic invoice attachment to WooCommerce emails.')
                ->addPanel([
                    'title' => 'Email Automation',
                    'icon' => 'dashicons-email-alt',
                    'fields' => [
                        [
                            'id' => 'attach_to_emails',
                            'label' => 'Attach Invoice to Emails',
                            'type' => 'checkbox',
                            'options' => [
                                'new_order' => 'New Order (Admin)',
                                'cancelled_order' => 'Cancelled Order (Admin)',
                                'failed_order' => 'Failed Order (Admin)',
                                'customer_on_hold_order' => 'Order On-Hold (Customer)',
                                'customer_processing_order' => 'Processing Order (Customer)',
                                'customer_completed_order' => 'Completed Order (Customer)',
                                'customer_refunded_order' => 'Refunded Order (Customer)',
                                'customer_invoice' => 'Customer Invoice',
                            ],
                            'description' => 'Automatically attach PDF invoice to selected emails.',
                        ],
                        [
                            'id' => 'auto_generate_on_status',
                            'label' => 'Auto-Generate on Status',
                            'type' => 'checkbox',
                            'options' => [
                                'pending' => 'Pending Payment',
                                'processing' => 'Processing',
                                'on-hold' => 'On Hold',
                                'completed' => 'Completed',
                                'refunded' => 'Refunded',
                            ],
                            'description' => 'Auto-generate invoice when order reaches selected status.',
                        ],
                    ]
                ]);

            $tab->tabLink('Access Control', 'Control how customers can access their invoices.')
                ->addPanel([
                    'title' => 'Access Control',
                    'icon' => 'dashicons-lock',
                    'fields' => [
                        [
                            'id' => 'customer_print_access',
                            'label' => 'Customer Print Access',
                            'type' => 'radio',
                            'options' => [
                                'logged_in' => 'Logged-in customers only',
                                'guest_token' => 'Allow guests with secure token',
                                'disabled' => 'Disabled (admin only)',
                            ],
                            'description' => 'Control how customers can access their invoices.',
                        ],
                    ]
                ]);

            $tab->tabLink('Invoice Numbering', 'Configure invoice number format and sequencing.')
                ->addPanel([
                    'title' => 'Invoice Numbering',
                    'icon' => 'dashicons-editor-ol',
                    'fields' => [
                        [
                            'id' => 'invoice_number_prefix',
                            'label' => 'Prefix',
                            'type' => 'text',
                            'placeholder' => 'INV-',
                            'description' => 'Text before the number (e.g., INV-, {Y}- for year).',
                        ],
                        [
                            'id' => 'invoice_number_suffix',
                            'label' => 'Suffix',
                            'type' => 'text',
                            'placeholder' => '',
                            'description' => 'Text after the number (e.g., -A, /{Y} for year).',
                        ],
                    ]
                ]);

            $tab->tabLink('Invoice Date', '')
                ->addPanel([
                    'title' => 'Invoice Date Settings',
                    'icon' => 'dashicons-calendar-alt',
                    'fields' => [
                        [
                            'id' => 'invoice_date_source',
                            'label' => 'Date Source',
                            'type' => 'radio',
                            'options' => [
                                'order' => 'Order Date',
                                'invoice' => 'Invoice Generation Date',
                            ],
                            'description' => 'Which date to display as invoice date.',
                        ],
                        [
                            'id' => 'invoice_date_format',
                            'label' => 'Date Format',
                            'type' => 'text',
                            'placeholder' => 'Y-m-d',
                            'description' => 'PHP date format (e.g., Y-m-d, d/m/Y, F j, Y).',
                        ],
                        [
                            'id' => 'invoice_date_label',
                            'label' => 'Invoice Date Label',
                            'type' => 'text',
                            'placeholder' => 'Invoice Date',
                            'description' => 'Label shown before invoice date.',
                        ],
                        [
                            'id' => 'order_date_label',
                            'label' => 'Order Date Label',
                            'type' => 'text',
                            'placeholder' => 'Order Date',
                            'description' => 'Label shown before order date.',
                        ],
                    ]
                ]);
        })->render(30);
    }
}
