-- Add menu permissions table
CREATE TABLE IF NOT EXISTS `menu_permissions` (
    `id` INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    `role` ENUM(
        'Owner',
        'Admin',
        'Kasir',
        'Staff Gudang'
    ) NOT NULL,
    `menu_key` VARCHAR(50) NOT NULL COMMENT 'Unique identifier for menu item',
    `is_visible` BOOLEAN DEFAULT TRUE,
    `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    UNIQUE KEY unique_role_menu (`role`, `menu_key`),
    INDEX idx_role (`role`)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_unicode_ci;

-- Insert default permissions (all visible by default)
INSERT INTO
    `menu_permissions` (
        `role`,
        `menu_key`,
        `is_visible`
    )
VALUES
    -- Owner - Full Access
    ('Owner', 'dashboard', 1),
    ('Owner', 'pos', 1),
    ('Owner', 'sales', 1),
    ('Owner', 'purchases', 1),
    ('Owner', 'returns', 1),
    ('Owner', 'products', 1),
    ('Owner', 'inventory', 1),
    ('Owner', 'contacts', 1),
    ('Owner', 'cashbox', 1),
    ('Owner', 'income_expense', 1),
    ('Owner', 'receivables', 1),
    ('Owner', 'olshop_orders', 1),
    ('Owner', 'olshop_settings', 1),
    ('Owner', 'reports_sales', 1),
    ('Owner', 'reports_profit', 1),
    ('Owner', 'settings_shop', 1),
    ('Owner', 'settings_users', 1),
    (
        'Owner',
        'settings_permissions',
        1
    ),

-- Admin - Management Access
('Admin', 'dashboard', 1),
('Admin', 'pos', 1),
('Admin', 'sales', 1),
('Admin', 'purchases', 1),
('Admin', 'returns', 1),
('Admin', 'products', 1),
('Admin', 'inventory', 1),
('Admin', 'contacts', 1),
('Admin', 'cashbox', 1),
('Admin', 'income_expense', 1),
('Admin', 'receivables', 1),
('Admin', 'olshop_orders', 1),
('Admin', 'olshop_settings', 1),
('Admin', 'reports_sales', 1),
('Admin', 'reports_profit', 1),
('Admin', 'settings_shop', 1),
('Admin', 'settings_users', 1),
(
    'Admin',
    'settings_permissions',
    0
),

-- Kasir - Sales Focused
('Kasir', 'dashboard', 1),
('Kasir', 'pos', 1),
('Kasir', 'sales', 1),
('Kasir', 'purchases', 0),
('Kasir', 'returns', 1),
('Kasir', 'products', 1),
('Kasir', 'inventory', 0),
('Kasir', 'contacts', 1),
('Kasir', 'cashbox', 1),
('Kasir', 'income_expense', 0),
('Kasir', 'receivables', 1),
('Kasir', 'olshop_orders', 0),
('Kasir', 'olshop_settings', 0),
('Kasir', 'reports_sales', 1),
('Kasir', 'reports_profit', 0),
('Kasir', 'settings_shop', 0),
('Kasir', 'settings_users', 0),
(
    'Kasir',
    'settings_permissions',
    0
),

-- Staff Gudang - Inventory Focused
(
    'Staff Gudang',
    'dashboard',
    1
),
('Staff Gudang', 'pos', 0),
('Staff Gudang', 'sales', 0),
(
    'Staff Gudang',
    'purchases',
    1
),
('Staff Gudang', 'returns', 1),
('Staff Gudang', 'products', 1),
(
    'Staff Gudang',
    'inventory',
    1
),
('Staff Gudang', 'contacts', 1),
('Staff Gudang', 'cashbox', 0),
(
    'Staff Gudang',
    'income_expense',
    0
),
(
    'Staff Gudang',
    'receivables',
    0
),
(
    'Staff Gudang',
    'olshop_orders',
    1
),
(
    'Staff Gudang',
    'olshop_settings',
    0
),
(
    'Staff Gudang',
    'reports_sales',
    0
),
(
    'Staff Gudang',
    'reports_profit',
    0
),
(
    'Staff Gudang',
    'settings_shop',
    0
),
(
    'Staff Gudang',
    'settings_users',
    0
),
(
    'Staff Gudang',
    'settings_permissions',
    0
);