-- Migration 044: Action Menu System - Phase 2 & 3 Implementation
-- Phase 2: Manufacturing & Inventory (Work Orders, BOMs, Stock Movements)
-- Phase 3: HR & CRM (Employees, Opportunities, Support Tickets)

-- ============================================================================
-- PHASE 2: Work Order Detail (workorder_detail)
-- ============================================================================
INSERT INTO page_actions (page_identifier, action_name, icon, action_type, action_target, action_method, permission_required, css_class, display_order, group_name, is_active)
VALUES
-- Notes modal (reuses internal notes modal)
('workorder_detail', 'Notes', 'bi bi-chat-left-text', 'modal', 'notesModal', NULL, NULL, 'btn-outline-primary', 10, 'main', 1),
-- Print WO
('workorder_detail', 'Print WO', 'bi bi-printer', 'javascript', 'window.print()', NULL, NULL, 'btn-outline-theme', 20, 'main', 1),
-- Status change actions group
('workorder_detail', 'Start Work Order', 'fas fa-play', 'javascript', 'changeStatus(''in_progress'')', NULL, 'manufacturing.manage', 'btn-outline-success', 30, 'main', 1),
('workorder_detail', 'Mark Complete', 'fas fa-check', 'javascript', 'changeStatus(''completed'')', NULL, 'manufacturing.manage', 'btn-outline-success', 40, 'main', 1),
('workorder_detail', 'Cancel', 'fas fa-times', 'javascript', 'changeStatus(''cancelled'')', NULL, 'manufacturing.manage', 'btn-outline-danger', 50, 'main', 1),
-- Divider
('workorder_detail', NULL, NULL, 'divider', NULL, NULL, NULL, NULL, 55, 'main', 1),
-- Shop Floor link
('workorder_detail', 'Open in Shop Floor', 'fas fa-industry', 'link', 'manufacturing/shopfloor/workorder/{id}', NULL, 'manufacturing.view', 'btn-outline-info', 60, 'main', 1),
-- Back button
('workorder_detail', 'Back', 'fas fa-arrow-left', 'link', 'manufacturing/workorders', NULL, NULL, 'btn-outline-secondary', 100, 'back', 1);

-- Conditions for status change actions (hide Start if already in progress/completed, etc.)
INSERT INTO action_conditions (action_id, condition_type, field_name, field_value, custom_function, logic_operator)
SELECT id, 'field_not_equals', 'status', 'in_progress', NULL, 'AND' FROM page_actions WHERE page_identifier = 'workorder_detail' AND action_name = 'Start Work Order'
UNION ALL
SELECT id, 'field_not_equals', 'status', 'completed', NULL, 'AND' FROM page_actions WHERE page_identifier = 'workorder_detail' AND action_name = 'Start Work Order'
UNION ALL
SELECT id, 'field_not_equals', 'status', 'cancelled', NULL, 'AND' FROM page_actions WHERE page_identifier = 'workorder_detail' AND action_name = 'Start Work Order'
UNION ALL
SELECT id, 'field_not_equals', 'status', 'completed', NULL, 'AND' FROM page_actions WHERE page_identifier = 'workorder_detail' AND action_name = 'Mark Complete'
UNION ALL
SELECT id, 'field_not_equals', 'status', 'cancelled', NULL, 'AND' FROM page_actions WHERE page_identifier = 'workorder_detail' AND action_name = 'Mark Complete'
UNION ALL
SELECT id, 'field_not_equals', 'status', 'completed', NULL, 'AND' FROM page_actions WHERE page_identifier = 'workorder_detail' AND action_name = 'Cancel'
UNION ALL
SELECT id, 'field_not_equals', 'status', 'cancelled', NULL, 'AND' FROM page_actions WHERE page_identifier = 'workorder_detail' AND action_name = 'Cancel';

-- ============================================================================
-- PHASE 2: BOM Detail (bom_detail)
-- ============================================================================
INSERT INTO page_actions (page_identifier, action_name, icon, action_type, action_target, action_method, permission_required, css_class, display_order, group_name, is_active)
VALUES
-- Notes modal
('bom_detail', 'Notes', 'bi bi-chat-left-text', 'modal', 'notesModal', NULL, NULL, 'btn-outline-primary', 10, 'main', 1),
-- Print BOM
('bom_detail', 'Print BOM', 'bi bi-printer', 'javascript', 'window.print()', NULL, NULL, 'btn-outline-theme', 20, 'main', 1),
-- Back button
('bom_detail', 'Back', 'bi bi-arrow-left', 'link', 'manufacturing/boms', NULL, NULL, 'btn-outline-theme', 100, 'back', 1);

-- ============================================================================
-- PHASE 2: Stock Movement Detail (movement_detail)
-- ============================================================================
INSERT INTO page_actions (page_identifier, action_name, icon, action_type, action_target, action_method, permission_required, css_class, display_order, group_name, is_active)
VALUES
-- Notes modal
('movement_detail', 'Notes', 'bi bi-chat-left-text', 'modal', 'notesModal', NULL, NULL, 'btn-outline-primary', 10, 'main', 1),
-- Quick action links (these are in sidebar currently, moving to action menu)
('movement_detail', 'View Stock Levels', 'fas fa-boxes', 'link', 'inventory/stock-levels?product_id={product_id}', NULL, NULL, 'btn-outline-primary', 20, 'main', 1),
('movement_detail', 'Product Movement History', 'fas fa-history', 'link', 'inventory/movements?product_id={product_id}', NULL, NULL, 'btn-outline-secondary', 30, 'main', 1),
('movement_detail', 'Location Movements', 'fas fa-warehouse', 'link', 'inventory/movements?location_id={location_id}', NULL, NULL, 'btn-outline-info', 40, 'main', 1),
('movement_detail', 'Record New Movement', 'fas fa-plus', 'link', 'inventory/movements/create', NULL, 'inventory.manage', 'btn-outline-success', 50, 'main', 1),
-- Back button
('movement_detail', 'Back', 'fas fa-arrow-left', 'link', 'inventory/movements', NULL, NULL, 'btn-cancel', 100, 'back', 1);

-- ============================================================================
-- PHASE 3: Employee Detail (employee_detail)
-- ============================================================================
INSERT INTO page_actions (page_identifier, action_name, icon, action_type, action_target, action_method, permission_required, css_class, display_order, group_name, is_active)
VALUES
-- Notes modal
('employee_detail', 'Notes', 'bi bi-chat-left-text', 'modal', 'notesModal', NULL, NULL, 'btn-outline-primary', 10, 'main', 1),
-- Edit button
('employee_detail', 'Edit', 'bi bi-pencil', 'link', 'employees/{id}/edit', NULL, 'hr.edit', 'btn-outline-warning', 20, 'main', 1),
-- Back button
('employee_detail', 'Back', 'bi bi-arrow-left', 'link', 'employees', NULL, NULL, 'btn-outline-secondary', 100, 'back', 1);

-- ============================================================================
-- PHASE 3: Opportunity Detail (opportunity_detail)
-- ============================================================================
INSERT INTO page_actions (page_identifier, action_name, icon, action_type, action_target, action_method, permission_required, css_class, display_order, group_name, is_active)
VALUES
-- Notes modal
('opportunity_detail', 'Notes', 'bi bi-chat-left-text', 'modal', 'notesModal', NULL, NULL, 'btn-outline-primary', 10, 'main', 1),
-- Edit button
('opportunity_detail', 'Edit', 'bi bi-pencil', 'link', 'crm/opportunities/{id}/edit', NULL, 'crm.edit', 'btn-outline-theme', 20, 'main', 1),
-- Email actions
('opportunity_detail', 'Send Email', 'bi bi-envelope', 'modal', 'emailModal', NULL, NULL, 'btn-outline-primary', 30, 'main', 1),
('opportunity_detail', 'Log Received Email', 'bi bi-envelope-check', 'modal', 'logEmailModal', NULL, NULL, 'btn-outline-secondary', 40, 'main', 1),
-- Clone opportunity
('opportunity_detail', 'Clone', 'bi bi-files', 'link', 'crm/opportunities/{id}/clone', NULL, 'crm.create', 'btn-outline-secondary', 50, 'main', 1),
-- Back button
('opportunity_detail', 'Back', 'bi bi-arrow-left', 'link', 'crm/opportunities', NULL, NULL, 'btn-outline-secondary', 100, 'back', 1);

-- ============================================================================
-- PHASE 3: Support Ticket Detail (ticket_detail)
-- ============================================================================
INSERT INTO page_actions (page_identifier, action_name, icon, action_type, action_target, action_method, permission_required, css_class, display_order, group_name, is_active)
VALUES
-- Notes modal
('ticket_detail', 'Notes', 'bi bi-chat-left-text', 'modal', 'notesModal', NULL, NULL, 'btn-outline-primary', 10, 'main', 1),
-- Edit button
('ticket_detail', 'Edit', 'bi bi-pencil', 'link', 'crm/support/{id}/edit', NULL, 'crm.edit', 'btn-outline-theme', 20, 'main', 1),
-- Back button
('ticket_detail', 'Back', 'bi bi-arrow-left', 'link', 'crm/support', NULL, NULL, 'btn-outline-secondary', 100, 'back', 1);

-- Migration complete
-- Run this file: mysql -u root -p brickwal_m1_ds < 044_phase2_phase3_action_menus.sql
