-- ============================================================================
-- Phase 4: Settings Modules Definitions (Declarative)
-- ============================================================================
-- These modules demonstrate additional ActionRegistry and platform features
-- for settings management interfaces.
-- ============================================================================

-- ============================================================================
-- Quotation Settings Module
-- ============================================================================

INSERT INTO modules (
    name,
    config,
    icon,
    aria_label,
    description
) VALUES (
    'Quotation Settings',
    '{
        "title": "Quotation Settings",
        "modulePath": "settings/quotations",
        "body": [
            {
                "type": "form",
                "id": "quotationSettingsForm",
                "formName": "Quotation Settings",
                "options": {
                    "loadRecord": {
                        "table": "quotation_settings",
                        "orgScope": true
                    }
                }
            }
        ],
        "footer": [
            {
                "type": "button",
                "label": "Cancel",
                "class": "btn btn-ghost",
                "action": "close"
            },
            {
                "type": "button",
                "label": "Save",
                "icon": "fa-save",
                "class": "btn btn-primary",
                "action": "save",
                "ariaLabel": "Save quotation settings"
            }
        ]
    }'::jsonb,
    'fa-cog',
    'Quotation Settings',
    'Configure quotation defaults, numbering, and templates'
) ON CONFLICT (name) DO UPDATE SET
    config = EXCLUDED.config,
    icon = EXCLUDED.icon,
    aria_label = EXCLUDED.aria_label,
    description = EXCLUDED.description,
    updatedAt = now();

-- ============================================================================
-- Invoice Settings Module
-- ============================================================================

INSERT INTO modules (
    name,
    config,
    icon,
    aria_label,
    description
) VALUES (
    'Invoice Settings',
    '{
        "title": "Invoice Settings",
        "modulePath": "settings/invoices",
        "body": [
            {
                "type": "tabs",
                "tabs": [
                    {
                        "label": "General",
                        "icon": "fa-cog",
                        "elements": [
                            {
                                "type": "form",
                                "id": "invoiceSettingsForm",
                                "formName": "Invoice Settings",
                                "options": {
                                    "loadRecord": {
                                        "table": "invoice_settings",
                                        "orgScope": true
                                    }
                                }
                            }
                        ]
                    },
                    {
                        "label": "TaxCore",
                        "icon": "fa-file-invoice-dollar",
                        "elements": [
                            {
                                "type": "alert",
                                "class": "alert alert-info",
                                "icon": "fa-info-circle",
                                "text": "Configure TaxCore integration for automated tax submissions."
                            },
                            {
                                "type": "form",
                                "id": "taxcoreSettingsForm",
                                "formName": "TaxCore Settings",
                                "options": {
                                    "loadRecord": {
                                        "table": "taxcore_settings",
                                        "orgScope": true
                                    }
                                }
                            },
                            {
                                "type": "divider"
                            },
                            {
                                "type": "button",
                                "label": "Test TaxCore Connection",
                                "icon": "fa-plug",
                                "class": "btn btn-secondary",
                                "action": "taxcore:test-connection",
                                "ariaLabel": "Test TaxCore API connection"
                            }
                        ]
                    },
                    {
                        "label": "Templates",
                        "icon": "fa-file-alt",
                        "elements": [
                            {
                                "type": "button",
                                "label": "Add Template",
                                "icon": "fa-plus",
                                "class": "btn btn-primary mb-4",
                                "action": {
                                    "handler": "openTemplateForm"
                                }
                            },
                            {
                                "type": "table",
                                "id": "invoiceTemplates",
                                "options": {
                                    "tableName": "invoice_templates",
                                    "showGlobalSearch": false,
                                    "addButton": false,
                                    "rowActions": [
                                        {
                                            "label": "Edit",
                                            "icon": "fa-edit",
                                            "action": {
                                                "handler": "openTemplateForm"
                                            }
                                        },
                                        {
                                            "label": "Preview",
                                            "icon": "fa-eye",
                                            "action": "templates:preview"
                                        },
                                        {
                                            "label": "Set Default",
                                            "icon": "fa-check",
                                            "action": "templates:set-default",
                                            "condition": "is_default != true"
                                        },
                                        {
                                            "label": "Delete",
                                            "icon": "fa-trash",
                                            "class": "btn btn-error btn-xs",
                                            "action": "records:delete",
                                            "condition": "is_default != true",
                                            "confirm": "Delete this template?"
                                        }
                                    ]
                                }
                            }
                        ]
                    }
                ]
            }
        ],
        "footer": [
            {
                "type": "button",
                "label": "Close",
                "class": "btn btn-ghost",
                "action": "close"
            },
            {
                "type": "button",
                "label": "Save All",
                "icon": "fa-save",
                "class": "btn btn-primary",
                "action": "save",
                "confirm": "Save all settings?",
                "ariaLabel": "Save all invoice settings"
            }
        ]
    }'::jsonb,
    'fa-cog',
    'Invoice Settings',
    'Configure invoice defaults, TaxCore integration, and templates'
) ON CONFLICT (name) DO UPDATE SET
    config = EXCLUDED.config,
    icon = EXCLUDED.icon,
    aria_label = EXCLUDED.aria_label,
    description = EXCLUDED.description,
    updatedAt = now();

-- ============================================================================
-- Payment Methods Module
-- ============================================================================

INSERT INTO modules (
    name,
    config,
    icon,
    aria_label,
    description
) VALUES (
    'Payment Methods',
    '{
        "title": "Payment Methods",
        "modulePath": "settings/payment-methods",
        "header": [
            {
                "type": "button",
                "label": "Add Payment Method",
                "icon": "fa-plus",
                "class": "btn btn-primary",
                "action": {
                    "handler": "openPaymentMethodForm"
                },
                "ariaLabel": "Add new payment method"
            }
        ],
        "body": [
            {
                "type": "table",
                "id": "paymentMethodsTable",
                "options": {
                    "tableName": "payment_methods",
                    "showGlobalSearch": false,
                    "columnsToShow": ["name", "type", "is_active", "display_order"],
                    "columnLabels": {
                        "name": "Name",
                        "type": "Type",
                        "is_active": "Active",
                        "display_order": "Order"
                    },
                    "rowActions": [
                        {
                            "label": "Edit",
                            "icon": "fa-edit",
                            "class": "btn btn-info btn-xs",
                            "action": {
                                "handler": "openPaymentMethodForm"
                            }
                        },
                        {
                            "label": "Activate",
                            "icon": "fa-check",
                            "class": "btn btn-success btn-xs",
                            "action": "payment-methods:activate",
                            "condition": "is_active == false"
                        },
                        {
                            "label": "Deactivate",
                            "icon": "fa-ban",
                            "class": "btn btn-warning btn-xs",
                            "action": "payment-methods:deactivate",
                            "condition": "is_active == true",
                            "confirm": "Deactivate this payment method?"
                        },
                        {
                            "label": "Delete",
                            "icon": "fa-trash",
                            "class": "btn btn-error btn-xs",
                            "action": "records:delete",
                            "confirm": "Delete this payment method?"
                        }
                    ]
                }
            }
        ],
        "footer": [
            {
                "type": "button",
                "label": "Close",
                "class": "btn btn-ghost",
                "action": "close"
            }
        ]
    }'::jsonb,
    'fa-credit-card',
    'Payment Methods',
    'Manage available payment methods for invoices and quotations'
) ON CONFLICT (name) DO UPDATE SET
    config = EXCLUDED.config,
    icon = EXCLUDED.icon,
    aria_label = EXCLUDED.aria_label,
    description = EXCLUDED.description,
    updatedAt = now();

-- ============================================================================
-- Products Module (Supporting Module)
-- ============================================================================

INSERT INTO modules (
    name,
    config,
    icon,
    aria_label,
    description
) VALUES (
    'Products',
    '{
        "title": "Products",
        "modulePath": "inventory/products",
        "header": [
            {
                "type": "button",
                "label": "New Product",
                "icon": "fa-plus",
                "class": "btn btn-primary",
                "action": {
                    "handler": "openProductForm"
                },
                "ariaLabel": "Create new product"
            },
            {
                "type": "filter-select",
                "options": {
                    "data_column": "is_active",
                    "label": "Status",
                    "options": [
                        {"label": "All", "value": ""},
                        {"label": "Active", "value": "true"},
                        {"label": "Inactive", "value": "false"}
                    ]
                }
            }
        ],
        "body": [
            {
                "type": "table",
                "id": "productsTable",
                "options": {
                    "tableName": "products",
                    "showGlobalSearch": true,
                    "columnsToShow": ["name", "sku", "price", "tax_rate", "is_active"],
                    "columnLabels": {
                        "name": "Name",
                        "sku": "SKU",
                        "price": "Price",
                        "tax_rate": "Tax Rate",
                        "is_active": "Active"
                    },
                    "rowActions": [
                        {
                            "label": "Edit",
                            "icon": "fa-edit",
                            "class": "btn btn-info btn-xs",
                            "action": {
                                "handler": "openProductForm"
                            }
                        },
                        {
                            "label": "Activate",
                            "icon": "fa-check",
                            "class": "btn btn-success btn-xs",
                            "action": "products:activate",
                            "condition": "is_active == false"
                        },
                        {
                            "label": "Deactivate",
                            "icon": "fa-ban",
                            "class": "btn btn-warning btn-xs",
                            "action": "products:deactivate",
                            "condition": "is_active == true"
                        },
                        {
                            "label": "Delete",
                            "icon": "fa-trash",
                            "class": "btn btn-error btn-xs",
                            "action": "records:delete",
                            "confirm": "Delete this product?"
                        }
                    ]
                }
            }
        ],
        "footer": [
            {
                "type": "button",
                "label": "Close",
                "class": "btn btn-ghost",
                "action": "close"
            }
        ],
        "dataSources": {
            "products": {
                "table": "products",
                "syncPolicy": "default",
                "autoRefresh": true
            }
        }
    }'::jsonb,
    'fa-box',
    'Products',
    'Manage products for quotations and invoices'
) ON CONFLICT (name) DO UPDATE SET
    config = EXCLUDED.config,
    icon = EXCLUDED.icon,
    aria_label = EXCLUDED.aria_label,
    description = EXCLUDED.description,
    updatedAt = now();

-- ============================================================================
-- Services Module (Supporting Module)
-- ============================================================================

INSERT INTO modules (
    name,
    config,
    icon,
    aria_label,
    description
) VALUES (
    'Services',
    '{
        "title": "Services",
        "modulePath": "inventory/services",
        "header": [
            {
                "type": "button",
                "label": "New Service",
                "icon": "fa-plus",
                "class": "btn btn-primary",
                "action": {
                    "handler": "openServiceForm"
                },
                "ariaLabel": "Create new service"
            }
        ],
        "body": [
            {
                "type": "table",
                "id": "servicesTable",
                "options": {
                    "tableName": "services",
                    "showGlobalSearch": true,
                    "columnsToShow": ["name", "code", "price", "tax_rate", "is_active"],
                    "columnLabels": {
                        "name": "Name",
                        "code": "Code",
                        "price": "Price",
                        "tax_rate": "Tax Rate",
                        "is_active": "Active"
                    },
                    "rowActions": [
                        {
                            "label": "Edit",
                            "icon": "fa-edit",
                            "class": "btn btn-info btn-xs",
                            "action": {
                                "handler": "openServiceForm"
                            }
                        },
                        {
                            "label": "Delete",
                            "icon": "fa-trash",
                            "class": "btn btn-error btn-xs",
                            "action": "records:delete",
                            "confirm": "Delete this service?"
                        }
                    ]
                }
            }
        ],
        "footer": [
            {
                "type": "button",
                "label": "Close",
                "class": "btn btn-ghost",
                "action": "close"
            }
        ]
    }'::jsonb,
    'fa-wrench',
    'Services',
    'Manage services for quotations and invoices'
) ON CONFLICT (name) DO UPDATE SET
    config = EXCLUDED.config,
    icon = EXCLUDED.icon,
    aria_label = EXCLUDED.aria_label,
    description = EXCLUDED.description,
    updatedAt = now();

-- ============================================================================
-- Success!
-- ============================================================================
COMMENT ON TABLE modules IS 'Phase 4: Settings and supporting modules demonstrate additional ActionRegistry patterns and conditional actions';
