-- ============================================================================
-- Phase 4: Quotations Module Definition (Declarative)
-- ============================================================================
-- This module uses the ActionRegistry, ModalBuilder, and DataLoader
-- enhancements to provide a fully declarative quotations management interface.
--
-- Features:
-- - Status-based row actions (Send, Convert, Reject, etc.)
-- - Conditional action visibility
-- - ShoppingCartComponent for line items
-- - ActionRegistry integration for all actions
-- - No custom JavaScript required!
-- ============================================================================

-- Insert Quotations Module
INSERT INTO modules (
    name,
    config,
    icon,
    aria_label,
    description
) VALUES (
    'Quotations',
    '{
        "title": "Quotations Management",
        "modulePath": "quotations",
        "header": [
            {
                "type": "button",
                "label": "New Quotation",
                "icon": "fa-plus",
                "class": "btn btn-primary",
                "action": {
                    "handler": "openQuotationForm"
                },
                "ariaLabel": "Create new quotation"
            },
            {
                "type": "filter-select",
                "options": {
                    "data_column": "status",
                    "label": "Status",
                    "options": [
                        {"label": "All", "value": ""},
                        {"label": "Draft", "value": "draft"},
                        {"label": "Sent", "value": "sent"},
                        {"label": "Accepted", "value": "accepted"},
                        {"label": "Rejected", "value": "rejected"},
                        {"label": "Expired", "value": "expired"},
                        {"label": "Converted", "value": "converted"}
                    ]
                }
            },
            {
                "type": "filter-date-range",
                "options": {
                    "data_column": "date",
                    "label": "Date Range"
                }
            },
            {
                "type": "button",
                "label": "Refresh",
                "icon": "fa-sync",
                "class": "btn btn-ghost",
                "action": "refresh",
                "ariaLabel": "Refresh quotations list"
            }
        ],
        "body": [
            {
                "type": "table",
                "id": "quotationsTable",
                "options": {
                    "tableName": "quotations",
                    "primaryKeyField": "quotation_id",
                    "showGlobalSearch": true,
                    "columnsToShow": [
                        "quotation_number",
                        "date",
                        "customer_id",
                        "total_amount",
                        "tax_amount",
                        "status",
                        "sent_at",
                        "expires_at"
                    ],
                    "columnLabels": {
                        "quotation_number": "Quote #",
                        "date": "Date",
                        "customer_id": "Customer",
                        "total_amount": "Amount",
                        "tax_amount": "Tax",
                        "status": "Status",
                        "sent_at": "Sent",
                        "expires_at": "Expires"
                    },
                    "rowClick": "selectRecord",
                    "rowActions": [
                        {
                            "label": "View",
                            "icon": "fa-eye",
                            "class": "btn btn-info btn-xs",
                            "action": "openSelectedDetail",
                            "ariaLabel": "View quotation details"
                        },
                        {
                            "label": "Send",
                            "icon": "fa-paper-plane",
                            "class": "btn btn-primary btn-xs",
                            "action": "quotations:send",
                            "condition": "status == \"draft\"",
                            "confirm": "Send this quotation to the customer?",
                            "ariaLabel": "Send quotation to customer"
                        },
                        {
                            "label": "Convert",
                            "icon": "fa-file-invoice",
                            "class": "btn btn-success btn-xs",
                            "action": "quotations:convert",
                            "condition": "status == \"sent\" || status == \"accepted\"",
                            "confirm": "Convert this quotation to an invoice?",
                            "ariaLabel": "Convert quotation to invoice"
                        },
                        {
                            "label": "Accept",
                            "icon": "fa-check",
                            "class": "btn btn-success btn-xs",
                            "action": "quotations:accept",
                            "condition": "status == \"sent\"",
                            "confirm": "Accept this quotation?",
                            "ariaLabel": "Accept quotation"
                        },
                        {
                            "label": "Reject",
                            "icon": "fa-times",
                            "class": "btn btn-error btn-xs",
                            "action": "quotations:reject",
                            "condition": "status == \"sent\"",
                            "confirm": "Reject this quotation?",
                            "ariaLabel": "Reject quotation"
                        },
                        {
                            "label": "Duplicate",
                            "icon": "fa-copy",
                            "class": "btn btn-ghost btn-xs",
                            "action": "quotations:duplicate",
                            "ariaLabel": "Duplicate quotation"
                        },
                        {
                            "label": "PDF",
                            "icon": "fa-file-pdf",
                            "class": "btn btn-secondary btn-xs",
                            "action": "quotations:pdf",
                            "ariaLabel": "Download quotation PDF"
                        },
                        {
                            "label": "Delete",
                            "icon": "fa-trash",
                            "class": "btn btn-error btn-xs btn-outline",
                            "action": "records:delete",
                            "condition": "status == \"draft\" || status == \"expired\"",
                            "confirm": "Are you sure you want to delete this quotation?",
                            "ariaLabel": "Delete quotation"
                        }
                    ]
                }
            }
        ],
        "footer": [
            {
                "type": "button",
                "label": "Close",
                "class": "btn btn-ghost",
                "action": "close",
                "ariaLabel": "Close quotations module"
            }
        ],
        "dataSources": {
            "quotations": {
                "table": "quotations",
                "syncPolicy": "default",
                "autoRefresh": true,
                "refreshInterval": 30000
            }
        }
    }'::jsonb,
    'fa-file-alt',
    'Quotations Management',
    'Create, send, and manage quotations. Convert quotations to invoices.'
) ON CONFLICT (name) DO UPDATE SET
    config = EXCLUDED.config,
    icon = EXCLUDED.icon,
    aria_label = EXCLUDED.aria_label,
    description = EXCLUDED.description,
    updatedAt = now();

-- ============================================================================
-- Quotation Detail Modal Definition
-- ============================================================================
-- This modal shows full quotation details with action buttons
-- Uses ModalBuilder with ActionRegistry integration
-- ============================================================================

INSERT INTO modules (
    name,
    config,
    icon,
    aria_label,
    description
) VALUES (
    'Quotation Detail',
    '{
        "title": "Quotation {quotation_number}",
        "size": "large",
        "header": [
            {
                "type": "badge",
                "class": "badge badge-lg",
                "classExpression": "status == \"draft\" ? \"badge-warning\" : status == \"sent\" ? \"badge-info\" : status == \"accepted\" ? \"badge-success\" : status == \"rejected\" ? \"badge-error\" : \"badge-ghost\"",
                "text": "{status}"
            }
        ],
        "body": [
            {
                "type": "tabs",
                "tabs": [
                    {
                        "label": "Details",
                        "icon": "fa-info-circle",
                        "elements": [
                            {
                                "type": "grid",
                                "columns": 2,
                                "elements": [
                                    {
                                        "type": "field-display",
                                        "label": "Quotation Number",
                                        "field": "quotation_number"
                                    },
                                    {
                                        "type": "field-display",
                                        "label": "Date",
                                        "field": "date",
                                        "format": "date"
                                    },
                                    {
                                        "type": "field-display",
                                        "label": "Customer",
                                        "field": "customer_id",
                                        "lookup": {
                                            "table": "clients",
                                            "keyField": "client_id",
                                            "displayField": "name"
                                        }
                                    },
                                    {
                                        "type": "field-display",
                                        "label": "Status",
                                        "field": "status"
                                    },
                                    {
                                        "type": "field-display",
                                        "label": "Total Amount",
                                        "field": "total_amount",
                                        "format": "currency"
                                    },
                                    {
                                        "type": "field-display",
                                        "label": "Tax Amount",
                                        "field": "tax_amount",
                                        "format": "currency"
                                    },
                                    {
                                        "type": "field-display",
                                        "label": "Sent At",
                                        "field": "sent_at",
                                        "format": "datetime"
                                    },
                                    {
                                        "type": "field-display",
                                        "label": "Expires At",
                                        "field": "expires_at",
                                        "format": "datetime"
                                    }
                                ]
                            },
                            {
                                "type": "divider"
                            },
                            {
                                "type": "field-display",
                                "label": "Note",
                                "field": "note"
                            },
                            {
                                "type": "field-display",
                                "label": "Terms",
                                "field": "terms"
                            }
                        ]
                    },
                    {
                        "label": "Line Items",
                        "icon": "fa-list",
                        "elements": [
                            {
                                "type": "shopping-cart",
                                "id": "quotationLineItems",
                                "options": {
                                    "dataField": "line_items",
                                    "readonly": true,
                                    "columns": [
                                        {
                                            "field": "item_type",
                                            "label": "Type",
                                            "type": "select",
                                            "options": [
                                                {"value": "product", "label": "Product"},
                                                {"value": "service", "label": "Service"}
                                            ]
                                        },
                                        {
                                            "field": "item_id",
                                            "label": "Item",
                                            "type": "select",
                                            "options_source_field": "item_type",
                                            "options_tables": {
                                                "product": "products",
                                                "service": "services"
                                            },
                                            "auto_populate": {
                                                "description": "name",
                                                "unit_price": "price",
                                                "tax_rate": "tax_rate"
                                            }
                                        },
                                        {
                                            "field": "description",
                                            "label": "Description",
                                            "type": "text"
                                        },
                                        {
                                            "field": "quantity",
                                            "label": "Qty",
                                            "type": "number"
                                        },
                                        {
                                            "field": "unit_price",
                                            "label": "Price",
                                            "type": "number"
                                        },
                                        {
                                            "field": "tax_rate",
                                            "label": "Tax %",
                                            "type": "number"
                                        },
                                        {
                                            "field": "subtotal",
                                            "label": "Subtotal",
                                            "type": "computed",
                                            "formula": "quantity * unit_price"
                                        },
                                        {
                                            "field": "total",
                                            "label": "Total",
                                            "type": "computed",
                                            "formula": "(quantity * unit_price) * (1 + tax_rate/100)"
                                        }
                                    ]
                                }
                            }
                        ]
                    },
                    {
                        "label": "History",
                        "icon": "fa-history",
                        "elements": [
                            {
                                "type": "table",
                                "id": "quotationHistory",
                                "options": {
                                    "tableName": "audit_logs",
                                    "filters": {
                                        "table_name": "quotations",
                                        "record_id": "{quotation_id}"
                                    },
                                    "showGlobalSearch": false,
                                    "addButton": false,
                                    "columnsToShow": ["action", "changed_by", "changed_at", "changes"],
                                    "columnLabels": {
                                        "action": "Action",
                                        "changed_by": "User",
                                        "changed_at": "Date",
                                        "changes": "Changes"
                                    }
                                }
                            }
                        ]
                    }
                ]
            }
        ],
        "footer": [
            {
                "type": "button",
                "label": "Close",
                "class": "btn btn-ghost",
                "action": "close"
            },
            {
                "type": "button",
                "label": "Edit",
                "icon": "fa-edit",
                "class": "btn btn-secondary",
                "action": {
                    "handler": "openQuotationForm"
                },
                "condition": "status == \"draft\""
            },
            {
                "type": "button",
                "label": "Send",
                "icon": "fa-paper-plane",
                "class": "btn btn-primary",
                "action": "quotations:send",
                "condition": "status == \"draft\"",
                "confirm": "Send this quotation to the customer?"
            },
            {
                "type": "button",
                "label": "Convert to Invoice",
                "icon": "fa-file-invoice",
                "class": "btn btn-success",
                "action": "quotations:convert",
                "condition": "status == \"sent\" || status == \"accepted\"",
                "confirm": "Convert this quotation to an invoice?"
            },
            {
                "type": "button",
                "label": "Download PDF",
                "icon": "fa-file-pdf",
                "class": "btn btn-secondary",
                "action": "quotations:pdf"
            }
        ]
    }'::jsonb,
    'fa-file-alt',
    'Quotation Detail',
    'View and manage individual quotation details'
) 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: Quotations module definitions use ActionRegistry, ModalBuilder, and DataLoader enhancements for fully declarative configuration';
