-- ============================================================================
-- Migration: Branches Management Module
-- Date: 2025-11-17
-- Purpose: Add declarative branches management module with hierarchical
--          structure, performance analytics, and inter-branch transfers
-- Dependencies: Requires branches table and branch_form to exist
-- Related: sql/defaults.sql (branch_form), sql/modules.sql (module definition)
-- ============================================================================

INSERT INTO modules (
    module_id,
    name,
    config,
    org_id,
    branch_id,
    icon,
    aria_label,
    description
) VALUES (
    '019a8e8d-b254-7d24-8a58-3c75a595acc0'::uuid,
    'branches_management',
    '{
        "title": "Branches Management",
        "header": [
            {
                "type": "button",
                "label": "Add Branch",
                "icon": "fa-plus",
                "class": "btn btn-primary mb-4",
                "formName": "branch_form"
            }
        ],
        "body": [
            {
                "type": "table",
                "options": {
                    "tableName": "branches",
                    "showGlobalSearch": true,
                    "editFormName": "branch_form",
                    "columnsToShow": ["name", "code", "type", "parent_branch_name", "address"],
                    "columnLabels": {
                        "name": "Branch Name",
                        "code": "Branch Code",
                        "type": "Type",
                        "parent_branch_name": "Parent Branch",
                        "address": "Address"
                    },
                    "rowActions": [
                        {
                            "label": "Edit",
                            "icon": "fa-edit",
                            "action": "edit"
                        }
                    ]
                }
            }
        ],
        "footer": [
            {
                "type": "button",
                "label": "Close",
                "onClick": "closeModal"
            }
        ]
    }'::jsonb,
    NULL,
    NULL,
    'fa-building',
    'Branches Management',
    'Manage branches and locations with hierarchical structure and relationships'
);

-- Branch Form
INSERT INTO forms (
    name,
    description,
    mapped_table,
    columns,
    layout_type,
    elements
) VALUES (
    'branch_form',
    'Create and manage branch locations',
    'branches',
    2,
    'grid',
    '[
        {
            "name": "name",
            "label": "Branch Name",
            "type": "text",
            "required": true
        },
        {
            "name": "code",
            "label": "Branch Code",
            "type": "text",
            "required": true
        },
        {
            "name": "type",
            "label": "Location Type",
            "type": "select",
            "required": true,
            "options": [
                {"value": "branch", "label": "Branch Office"},
                {"value": "store", "label": "Store/Outlet"},
                {"value": "warehouse", "label": "Warehouse"},
                {"value": "transit", "label": "In Transit"}
            ]
        },
        {
            "name": "parent_branch_id",
            "label": "Parent Branch",
            "type": "select",
            "required": false,
            "options_table": "branches",
            "options_label_column": "name",
            "options_value_column": "branch_id",
            "options_where": "isDeleted = false",
            "help_text": "Leave empty for top-level branch. Select a parent to create a child branch (enables automatic inventory transfers between related branches)."
        },
        {
            "name": "address",
            "label": "Address",
            "type": "textarea",
            "required": false,
            "colspan": 2
        }
    ]'::jsonb
);