-- Migration 042: Action Menu System
-- Dynamic action menus pulled from database for all pages

-- Table: page_actions
-- Stores all available actions for different pages
CREATE TABLE IF NOT EXISTS page_actions (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    page_identifier VARCHAR(100) NOT NULL COMMENT 'e.g., quote_detail, customer_detail, invoice_detail',
    action_name VARCHAR(100) NULL COMMENT 'Display name for the action (NULL for dividers)',
    icon VARCHAR(50) NULL COMMENT 'Bootstrap icon class, e.g., bi-pen, bi-envelope',
    action_type ENUM('javascript', 'link', 'form_submit', 'modal', 'divider') NOT NULL DEFAULT 'link',
    action_target VARCHAR(255) NULL COMMENT 'JS function name, route, or form ID depending on action_type',
    action_method VARCHAR(10) NULL COMMENT 'For form_submit: GET, POST, etc.',
    permission_required VARCHAR(100) NULL COMMENT 'Permission needed to see this action',
    css_class VARCHAR(100) NULL COMMENT 'Additional CSS classes for styling',
    display_order INT NOT NULL DEFAULT 0 COMMENT 'Order in which actions appear',
    group_name VARCHAR(50) NULL COMMENT 'Group actions together with dividers',
    is_active TINYINT(1) NOT NULL DEFAULT 1,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    INDEX idx_page_identifier (page_identifier),
    INDEX idx_is_active (is_active),
    INDEX idx_display_order (display_order)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Table: action_conditions
-- Defines conditions that must be met for an action to be visible
CREATE TABLE IF NOT EXISTS action_conditions (
    id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    action_id INT UNSIGNED NOT NULL,
    condition_type ENUM('field_equals', 'field_not_equals', 'field_empty', 'field_not_empty', 'custom_function') NOT NULL,
    field_name VARCHAR(100) NULL COMMENT 'Field to check in context object, e.g., status, public_token',
    field_value TEXT NULL COMMENT 'Expected value for comparison',
    custom_function VARCHAR(100) NULL COMMENT 'PHP function name for custom evaluation',
    logic_operator ENUM('AND', 'OR') NOT NULL DEFAULT 'AND' COMMENT 'How to combine with other conditions',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (action_id) REFERENCES page_actions(id) ON DELETE CASCADE,
    INDEX idx_action_id (action_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Insert Quote Detail Page Actions
INSERT INTO page_actions (page_identifier, action_name, icon, action_type, action_target, permission_required, display_order, group_name) VALUES
-- Signature Actions Group
('quote_detail', 'Send for E-Signature', 'bi-pen', 'javascript', 'sendForSignature', 'sales.edit', 10, 'signature'),
('quote_detail', 'Email Quote Link', 'bi-envelope', 'javascript', 'showEmailModal', NULL, 20, 'signature'),
('quote_detail', 'Copy Signature Link', 'bi-link-45deg', 'javascript', 'copySignatureLink', NULL, 30, 'signature'),

-- Order Actions Group
('quote_detail', 'Convert to Order', 'bi-arrow-right-circle', 'form_submit', 'convertForm', 'sales.create', 40, 'order'),

-- Divider
('quote_detail', NULL, NULL, 'divider', NULL, NULL, 50, NULL),

-- Standard Actions Group
('quote_detail', 'Notes', 'bi-sticky', 'javascript', 'openNotes', NULL, 60, 'standard'),
('quote_detail', 'Print Quote', 'bi-printer', 'link', 'quotes/general/{id}/print', NULL, 70, 'standard'),
('quote_detail', 'Edit Quote', 'bi-pencil', 'link', 'quotes/general/{id}/edit', 'sales.edit', 80, 'standard'),

-- Divider
('quote_detail', NULL, NULL, 'divider', NULL, NULL, 90, NULL),

-- Navigation
('quote_detail', 'Back to Quotes', 'bi-arrow-left', 'link', 'quotes/general', NULL, 100, 'navigation');

-- Set up conditions for signature actions
SET @send_sig_id = (SELECT id FROM page_actions WHERE page_identifier = 'quote_detail' AND action_name = 'Send for E-Signature');
SET @email_link_id = (SELECT id FROM page_actions WHERE page_identifier = 'quote_detail' AND action_name = 'Email Quote Link');
SET @copy_link_id = (SELECT id FROM page_actions WHERE page_identifier = 'quote_detail' AND action_name = 'Copy Signature Link');
SET @convert_id = (SELECT id FROM page_actions WHERE page_identifier = 'quote_detail' AND action_name = 'Convert to Order');
SET @edit_id = (SELECT id FROM page_actions WHERE page_identifier = 'quote_detail' AND action_name = 'Edit Quote');

-- Conditions for "Send for E-Signature"
-- Show only if: status != 'converted' AND customer_response is empty
INSERT INTO action_conditions (action_id, condition_type, field_name, field_value, logic_operator) VALUES
(@send_sig_id, 'field_not_equals', 'status', 'converted', 'AND'),
(@send_sig_id, 'field_empty', 'customer_response', NULL, 'AND');

-- Conditions for "Email Quote Link" and "Copy Signature Link"
-- Show only if: public_token is not empty
INSERT INTO action_conditions (action_id, condition_type, field_name, field_value, logic_operator) VALUES
(@email_link_id, 'field_not_empty', 'public_token', NULL, 'AND'),
(@copy_link_id, 'field_not_empty', 'public_token', NULL, 'AND');

-- Conditions for "Convert to Order"
-- Show only if: status != 'converted'
INSERT INTO action_conditions (action_id, condition_type, field_name, field_value, logic_operator) VALUES
(@convert_id, 'field_not_equals', 'status', 'converted', 'AND');

-- Conditions for "Edit Quote"
-- Show only if: status != 'converted'
INSERT INTO action_conditions (action_id, condition_type, field_name, field_value, logic_operator) VALUES
(@edit_id, 'field_not_equals', 'status', 'converted', 'AND');

-- Example usage comment:
-- To get actions for a page: SELECT * FROM page_actions WHERE page_identifier = 'quote_detail' AND is_active = 1 ORDER BY display_order
-- To check conditions: JOIN with action_conditions and evaluate each condition against context data
