-- =========================================================
-- Seed Data - Roles, Permissions, Default Admin, Settings
-- =========================================================

-- ROLES
INSERT INTO roles (role_name, description, is_system) VALUES
('OWNER',   'Full access to everything', 1),
('MANAGER', 'Manages daily operations, stock, staff', 0),
('CASHIER', 'Takes orders and payments', 0),
('KITCHEN', 'Views orders for preparation', 0),
('RIDER',   'Delivery rider - sees assigned deliveries only', 0);

-- PERMISSION MODULES
INSERT INTO permissions (module_key, module_label) VALUES
('dashboard',  'Dashboard'),
('orders',     'Orders'),
('menu',       'Menu Management'),
('stock',      'Stock / Raw Materials'),
('delivery',   'Delivery Management'),
('customers',  'Customers'),
('payments',   'Payments'),
('finance',    'Finance / Accounts'),
('expenses',   'Expenses'),
('payroll',    'Payroll'),
('attendance', 'Attendance'),
('reports',    'Reports'),
('users',      'User Management'),
('roles',      'Roles & Permissions'),
('settings',   'System Settings'),
('backups',    'Backups'),
('audit_log',  'Audit Log');

-- ROLE PERMISSIONS: OWNER gets everything
INSERT INTO role_permissions (role_id, permission_id, can_view, can_add, can_edit, can_delete)
SELECT (SELECT id FROM roles WHERE role_name = 'OWNER'), id, 1, 1, 1, 1 FROM permissions;

-- MANAGER: everything except user/role management and settings delete
INSERT INTO role_permissions (role_id, permission_id, can_view, can_add, can_edit, can_delete)
SELECT (SELECT id FROM roles WHERE role_name = 'MANAGER'), id, 1, 1, 1,
    CASE WHEN module_key IN ('users','roles','settings','backups') THEN 0 ELSE 1 END
FROM permissions
WHERE module_key NOT IN ('users','roles');

-- CASHIER: orders, payments, customers, delivery view
INSERT INTO role_permissions (role_id, permission_id, can_view, can_add, can_edit, can_delete)
SELECT (SELECT id FROM roles WHERE role_name = 'CASHIER'), id, 1, 1, 1, 0
FROM permissions WHERE module_key IN ('dashboard','orders','customers','payments','delivery');

-- KITCHEN: orders view only
INSERT INTO role_permissions (role_id, permission_id, can_view, can_add, can_edit, can_delete)
SELECT (SELECT id FROM roles WHERE role_name = 'KITCHEN'), id, 1, 0, 1, 0
FROM permissions WHERE module_key IN ('dashboard','orders');

-- RIDER: delivery view/edit only (own assigned)
INSERT INTO role_permissions (role_id, permission_id, can_view, can_add, can_edit, can_delete)
SELECT (SELECT id FROM roles WHERE role_name = 'RIDER'), id, 1, 0, 1, 0
FROM permissions WHERE module_key IN ('dashboard','delivery');

-- DEFAULT OWNER USER (username: admin / password: Admin@123 -- CHANGE AFTER FIRST LOGIN)
-- password_hash below = password_hash('Admin@123', PASSWORD_DEFAULT) generated at setup time (placeholder, see install.php)
INSERT INTO users (full_name, username, email, phone, password_hash, role_id, is_active, is_protected)
VALUES ('System Owner', 'admin', 'owner@example.com', '0700000000',
    '$2y$10$abcdefghijklmnopqrstuuOZJ8w6h1B1hFhIuF9y0zN0T1rL1e9Vy', -- overwritten by install.php
    (SELECT id FROM roles WHERE role_name = 'OWNER'), 1, 1);

-- COMPANY SETTINGS
INSERT INTO settings (setting_key, setting_value) VALUES
('company_name', 'Rice & Curry - Anuradhapura'),
('company_address', 'Office Ground, Anuradhapura'),
('company_phone', '0700000000'),
('currency_symbol', 'LKR'),
('timezone', 'Asia/Colombo'),
('free_delivery_enabled', '1'),
('default_delivery_radius_km', '5'),
('logo_path', ''),
('order_number_prefix', 'ORD');

-- PAYMENT METHODS
INSERT INTO payment_methods (method_name, fee_percentage) VALUES
('CASH', 0),
('CARD', 2.5),
('ONLINE_TRANSFER', 0);

-- FINANCE ACCOUNTS
INSERT INTO finance_accounts (account_name, account_type, current_balance) VALUES
('Cash in Hand', 'CASH', 0),
('Bank Account', 'BANK', 0),
('Card Settlement Account', 'BANK', 0);

-- Link payment methods to finance accounts
UPDATE payment_methods SET finance_account_id = (SELECT id FROM finance_accounts WHERE account_name='Cash in Hand') WHERE method_name='CASH';
UPDATE payment_methods SET finance_account_id = (SELECT id FROM finance_accounts WHERE account_name='Card Settlement Account') WHERE method_name='CARD';
UPDATE payment_methods SET finance_account_id = (SELECT id FROM finance_accounts WHERE account_name='Bank Account') WHERE method_name='ONLINE_TRANSFER';

-- SAMPLE DELIVERY ZONE (Free Delivery within Anuradhapura town)
INSERT INTO delivery_zones (zone_name, radius_km, is_free_delivery, delivery_fee, is_active) VALUES
('Anuradhapura Town (Free Delivery)', 5.00, 1, 0, 1),
('Outside Town (Charged)', 15.00, 0, 200.00, 1);

-- SAMPLE MENU CATEGORY + ITEM (so dashboard isn't empty)
INSERT INTO menu_categories (category_name, display_order) VALUES ('Rice & Curry', 1), ('Beverages', 2);
INSERT INTO menu_items (category_id, item_name, base_price, track_stock) VALUES
((SELECT id FROM menu_categories WHERE category_name='Rice & Curry'), 'Chicken Rice & Curry', 450.00, 1),
((SELECT id FROM menu_categories WHERE category_name='Rice & Curry'), 'Vegetable Rice & Curry', 350.00, 1);
