-- ERP1 Seed Data
USE erp1;

INSERT INTO currencies (code, name_en, name_ar, symbol, decimals, is_base) VALUES
('SAR', 'Saudi Riyal', 'ريال سعودي', 'SAR', 2, 1),
('USD', 'US Dollar', 'دولار أمريكي', 'USD', 2, 0);

INSERT INTO companies (name_en, name_ar, cr_number, vat_number, address_en, address_ar, base_currency_code, timezone)
VALUES ('Demo Company', 'شركة تجريبية', '1010101010', '300000000000003', 'Riyadh, KSA', 'الرياض، السعودية', 'SAR', 'Asia/Riyadh');

SET @company_id = (SELECT id FROM companies WHERE name_en = 'Demo Company' LIMIT 1);

INSERT INTO branches (company_id, name_en, name_ar, address_en, address_ar, phone, is_head)
VALUES (@company_id, 'Head Office', 'المكتب الرئيسي', 'Riyadh, KSA', 'الرياض، السعودية', '+966000000000', 1);

SET @branch_id = (SELECT id FROM branches WHERE company_id = @company_id LIMIT 1);

INSERT INTO fiscal_years (company_id, name, start_date, end_date, status)
VALUES (@company_id, 'FY2026', '2026-01-01', '2026-12-31', 'open');

SET @fy_id = (SELECT id FROM fiscal_years WHERE company_id = @company_id LIMIT 1);

INSERT INTO fiscal_periods (fiscal_year_id, period_no, start_date, end_date, is_closed) VALUES
(@fy_id, 1, '2026-01-01', '2026-01-31', 0),
(@fy_id, 2, '2026-02-01', '2026-02-28', 0),
(@fy_id, 3, '2026-03-01', '2026-03-31', 0),
(@fy_id, 4, '2026-04-01', '2026-04-30', 0),
(@fy_id, 5, '2026-05-01', '2026-05-31', 0),
(@fy_id, 6, '2026-06-01', '2026-06-30', 0),
(@fy_id, 7, '2026-07-01', '2026-07-31', 0),
(@fy_id, 8, '2026-08-01', '2026-08-31', 0),
(@fy_id, 9, '2026-09-01', '2026-09-30', 0),
(@fy_id, 10, '2026-10-01', '2026-10-31', 0),
(@fy_id, 11, '2026-11-01', '2026-11-30', 0),
(@fy_id, 12, '2026-12-01', '2026-12-31', 0);

INSERT INTO taxes (company_id, name_en, name_ar, rate, is_active)
VALUES (@company_id, 'VAT 15%', 'ضريبة القيمة المضافة 15%', 15.00, 1);

INSERT INTO roles (company_id, name) VALUES (@company_id, 'Administrator');
SET @role_admin = (SELECT id FROM roles WHERE company_id = @company_id AND name = 'Administrator' LIMIT 1);

INSERT INTO permissions (perm_key, description) VALUES
('users.manage', 'Manage users and roles'),
('company.manage', 'Manage company settings'),
('contacts.manage', 'Manage contacts'),
('sales.manage', 'Manage sales'),
('purchase.manage', 'Manage purchases'),
('inventory.manage', 'Manage inventory'),
('accounting.manage', 'Manage accounting'),
('hr.manage', 'Manage HR'),
('fleet.manage', 'Manage fleet'),
('notes.manage', 'Manage notes and tasks'),
('attachments.manage', 'Manage attachments'),
('zatca.manage', 'Manage ZATCA'),
('money.view', 'View money manager module'),
('money.manage', 'Manage money manager module');

INSERT INTO role_permissions (role_id, permission_id)
SELECT @role_admin, id FROM permissions;

INSERT INTO users (company_id, branch_id, name, email, phone, password_hash, is_active)
VALUES (@company_id, @branch_id, 'Admin', 'admin@erp.local', '+966000000000', '$2y$10$Q90Jgvbq8frRFH2zgp0ro.GlQBNnSjI/Jx/k4G5k7/jM.vJe977u6', 1);

SET @admin_id = (SELECT id FROM users WHERE email = 'admin@erp.local' LIMIT 1);
INSERT INTO user_roles (user_id, role_id) VALUES (@admin_id, @role_admin);

INSERT INTO units_of_measure (company_id, name_en, name_ar, symbol) VALUES
(@company_id, 'Piece', 'قطعة', 'PCS'),
(@company_id, 'Hour', 'ساعة', 'HR');

INSERT INTO warehouses (company_id, branch_id, name_en, name_ar, address_en, address_ar)
VALUES (@company_id, @branch_id, 'Main Warehouse', 'المستودع الرئيسي', 'Riyadh, KSA', 'الرياض، السعودية');

INSERT INTO accounts (company_id, code, name_en, name_ar, type, parent_id, is_posting, currency_code) VALUES
(@company_id, '1000', 'Assets', 'الأصول', 'asset', NULL, 0, 'SAR'),
(@company_id, '1010', 'Cash', 'الصندوق', 'asset', (SELECT id FROM accounts WHERE company_id = @company_id AND code = '1000'), 1, 'SAR'),
(@company_id, '1020', 'Bank', 'البنك', 'asset', (SELECT id FROM accounts WHERE company_id = @company_id AND code = '1000'), 1, 'SAR'),
(@company_id, '1100', 'Accounts Receivable', 'الذمم المدينة', 'asset', (SELECT id FROM accounts WHERE company_id = @company_id AND code = '1000'), 1, 'SAR'),
(@company_id, '1200', 'Inventory', 'المخزون', 'asset', (SELECT id FROM accounts WHERE company_id = @company_id AND code = '1000'), 1, 'SAR'),
(@company_id, '2000', 'Liabilities', 'الالتزامات', 'liability', NULL, 0, 'SAR'),
(@company_id, '2100', 'Accounts Payable', 'الذمم الدائنة', 'liability', (SELECT id FROM accounts WHERE company_id = @company_id AND code = '2000'), 1, 'SAR'),
(@company_id, '2110', 'VAT Output', 'ضريبة المخرجات', 'liability', (SELECT id FROM accounts WHERE company_id = @company_id AND code = '2000'), 1, 'SAR'),
(@company_id, '2120', 'VAT Input', 'ضريبة المدخلات', 'asset', (SELECT id FROM accounts WHERE company_id = @company_id AND code = '1000'), 1, 'SAR'),
(@company_id, '3000', 'Equity', 'حقوق الملكية', 'equity', NULL, 0, 'SAR'),
(@company_id, '4000', 'Revenue', 'الإيرادات', 'revenue', NULL, 0, 'SAR'),
(@company_id, '4010', 'Sales', 'المبيعات', 'revenue', (SELECT id FROM accounts WHERE company_id = @company_id AND code = '4000'), 1, 'SAR'),
(@company_id, '5000', 'Expenses', 'المصروفات', 'expense', NULL, 0, 'SAR'),
(@company_id, '5010', 'Cost of Sales', 'تكلفة المبيعات', 'expense', (SELECT id FROM accounts WHERE company_id = @company_id AND code = '5000'), 1, 'SAR');

INSERT INTO cost_centers (company_id, code, name_en, name_ar)
VALUES (@company_id, 'MAIN', 'Main', 'الرئيسي');

INSERT INTO zatca_settings (company_id, environment, seller_name, vat_number, address)
VALUES (@company_id, 'sandbox', 'Demo Company', '300000000000003', 'Riyadh, KSA');

-- Lookup Lists
INSERT IGNORE INTO lookup_values (company_id, list_key, code, name_en, name_ar, sort_order) VALUES
(@company_id, 'company_type', 'LLC', 'LLC', NULL, 1),
(@company_id, 'company_type', 'Branch', 'Branch', NULL, 2),
(@company_id, 'company_type', 'SoleProp', 'Sole Proprietor', NULL, 3),
(@company_id, 'industry', 'Trading', 'Trading', NULL, 1),
(@company_id, 'industry', 'Services', 'Services', NULL, 2),
(@company_id, 'customer_group', 'KEY', 'Key Accounts', NULL, 1),
(@company_id, 'customer_segment', 'Corporate', 'Corporate', NULL, 1),
(@company_id, 'customer_segment', 'SMB', 'SMB', NULL, 2),
(@company_id, 'vendor_group', 'STRATEGIC', 'Strategic', NULL, 1),
(@company_id, 'vendor_segment', 'Local', 'Local', NULL, 1),
(@company_id, 'vendor_segment', 'International', 'International', NULL, 2),
(@company_id, 'shipping_method', 'Road', 'Road', NULL, 1),
(@company_id, 'shipping_method', 'Air', 'Air', NULL, 2),
(@company_id, 'delivery_priority', 'Normal', 'Normal', NULL, 1),
(@company_id, 'delivery_priority', 'Urgent', 'Urgent', NULL, 2),
(@company_id, 'po_type', 'Standard', 'Standard', NULL, 1),
(@company_id, 'po_type', 'Service', 'Service', NULL, 2),
(@company_id, 'price_control_type', 'Fixed', 'Fixed', NULL, 1),
(@company_id, 'price_control_type', 'Variable', 'Variable', NULL, 2),
(@company_id, 'invoice_matching_type', '2-Way', '2-Way', NULL, 1),
(@company_id, 'invoice_matching_type', '3-Way', '3-Way', NULL, 2),
(@company_id, 'invoice_matching_rule', 'Standard', 'Standard', NULL, 1),
(@company_id, 'approval_workflow', 'STD', 'Standard Approval', NULL, 1),
(@company_id, 'nda_reference', 'NDA-STD', 'Standard NDA', NULL, 1),
(@company_id, 'payment_terms', 'NET30', 'Net 30 Days', NULL, 1),
(@company_id, 'incoterms', 'EXW', 'EXW', NULL, 1),
(@company_id, 'incoterms', 'CIF', 'CIF', NULL, 2),
(@company_id, 'delivery_terms', 'Delivered', 'Delivered', NULL, 1),
(@company_id, 'accounting_standard', 'IFRS', 'IFRS', NULL, 1),
(@company_id, 'accounting_standard', 'GAAP', 'GAAP', NULL, 2),
(@company_id, 'chart_of_accounts', 'COA-STD', 'Standard COA', NULL, 1),
(@company_id, 'chart_of_accounts', 'COA-IFRS', 'IFRS COA', NULL, 2),
(@company_id, 'cost_center_structure', 'CC-DEP', 'By Department', NULL, 1),
(@company_id, 'cost_center_structure', 'CC-PROJ', 'By Project', NULL, 2),
(@company_id, 'profit_center_structure', 'PC-BU', 'By Business Unit', NULL, 1),
(@company_id, 'profit_center_structure', 'PC-REG', 'By Region', NULL, 2),
(@company_id, 'timezone', 'Asia/Riyadh', 'Asia/Riyadh (UTC+3)', NULL, 1),
(@company_id, 'timezone', 'Asia/Dubai', 'Asia/Dubai (UTC+4)', NULL, 2),
(@company_id, 'timezone', 'UTC', 'UTC', NULL, 3),
(@company_id, 'contact_role', 'PROC', 'Procurement', NULL, 1),
(@company_id, 'contact_role', 'FIN', 'Finance', NULL, 2),
(@company_id, 'contact_role', 'SALES', 'Sales', NULL, 3),
(@company_id, 'employee_category', 'FULLTIME', 'Full Time', NULL, 1),
(@company_id, 'employee_category', 'PARTTIME', 'Part Time', NULL, 2),
(@company_id, 'employee_category', 'CONTRACT', 'Contract', NULL, 3),
(@company_id, 'business_unit', 'OPS', 'Operations', NULL, 1),
(@company_id, 'business_unit', 'SALES', 'Sales', NULL, 2),
(@company_id, 'business_unit', 'HR', 'Human Resources', NULL, 3),
(@company_id, 'position_title', 'MGR', 'Manager', NULL, 1),
(@company_id, 'position_title', 'ANALYST', 'Analyst', NULL, 2),
(@company_id, 'grade_level', 'G1', 'Grade 1', NULL, 1),
(@company_id, 'grade_level', 'G2', 'Grade 2', NULL, 2),
(@company_id, 'marital_status', 'SINGLE', 'Single', NULL, 1),
(@company_id, 'marital_status', 'MARRIED', 'Married', NULL, 2),
(@company_id, 'blood_group', 'A+', 'A+', NULL, 1),
(@company_id, 'blood_group', 'B+', 'B+', NULL, 2),
(@company_id, 'blood_group', 'O+', 'O+', NULL, 3),
(@company_id, 'blood_group', 'AB+', 'AB+', NULL, 4),
(@company_id, 'blood_group', 'A-', 'A-', NULL, 5),
(@company_id, 'blood_group', 'B-', 'B-', NULL, 6),
(@company_id, 'blood_group', 'O-', 'O-', NULL, 7),
(@company_id, 'blood_group', 'AB-', 'AB-', NULL, 8),
(@company_id, 'visa_type', 'WORK', 'Work Visa', NULL, 1),
(@company_id, 'visa_type', 'BUSINESS', 'Business Visa', NULL, 2),
(@company_id, 'visa_type', 'VISIT', 'Visit Visa', NULL, 3),
(@company_id, 'product_type', 'product', 'Product', NULL, 1),
(@company_id, 'product_type', 'service', 'Service', NULL, 2),
(@company_id, 'plate_type', 'PRIVATE', 'Private', NULL, 1),
(@company_id, 'plate_type', 'COMMERCIAL', 'Commercial', NULL, 2),
(@company_id, 'vehicle_type', 'SEDAN', 'Sedan', NULL, 1),
(@company_id, 'vehicle_type', 'SUV', 'SUV', NULL, 2),
(@company_id, 'vehicle_type', 'TRUCK', 'Truck', NULL, 3),
(@company_id, 'risk_category', 'LOW', 'Low', NULL, 1),
(@company_id, 'risk_category', 'MEDIUM', 'Medium', NULL, 2),
(@company_id, 'risk_category', 'HIGH', 'High', NULL, 3),
(@company_id, 'file_category', 'CONTRACT', 'Contract', NULL, 1),
(@company_id, 'file_category', 'INVOICE', 'Invoice', NULL, 2),
(@company_id, 'file_category', 'HR', 'HR Document', NULL, 3),
(@company_id, 'document_type', 'AGREEMENT', 'Agreement', NULL, 1),
(@company_id, 'document_type', 'CERTIFICATE', 'Certificate', NULL, 2),
(@company_id, 'document_type', 'REPORT', 'Report', NULL, 3),
(@company_id, 'access_level', 'PUBLIC', 'Public', NULL, 1),
(@company_id, 'access_level', 'INTERNAL', 'Internal', NULL, 2),
(@company_id, 'access_level', 'RESTRICTED', 'Restricted', NULL, 3),
(@company_id, 'confidentiality_level', 'LOW', 'Low', NULL, 1),
(@company_id, 'confidentiality_level', 'MEDIUM', 'Medium', NULL, 2),
(@company_id, 'confidentiality_level', 'HIGH', 'High', NULL, 3),
(@company_id, 'data_classification', 'PUBLIC', 'Public', NULL, 1),
(@company_id, 'data_classification', 'INTERNAL', 'Internal', NULL, 2),
(@company_id, 'data_classification', 'CONFIDENTIAL', 'Confidential', NULL, 3),
(@company_id, 'sales_org', 'SO1', 'Sales Org 1', NULL, 1),
(@company_id, 'sales_org', 'SO2', 'Sales Org 2', NULL, 2),
(@company_id, 'distribution_channel', 'DIRECT', 'Direct', NULL, 1),
(@company_id, 'distribution_channel', 'ONLINE', 'Online', NULL, 2),
(@company_id, 'division', 'CONSUMER', 'Consumer', NULL, 1),
(@company_id, 'division', 'ENTERPRISE', 'Enterprise', NULL, 2),
(@company_id, 'branch_location', 'Riyadh', 'Riyadh', NULL, 1),
(@company_id, 'branch_location', 'Jeddah', 'Jeddah', NULL, 2);

-- Document Sequences
INSERT IGNORE INTO document_sequences (company_id, branch_id, module, prefix, next_number, padding, reset_period) VALUES
(@company_id, @branch_id, 'customer', 'CUST-', 1, 5, NULL),
(@company_id, @branch_id, 'vendor', 'VEND-', 1, 5, NULL),
(@company_id, @branch_id, 'rfq', 'RFQ-', 1, 5, NULL),
(@company_id, @branch_id, 'rfq_ref', 'RRFQ-', 1, 5, NULL),
(@company_id, @branch_id, 'po', 'PO-', 1, 5, NULL),
(@company_id, @branch_id, 'quote', 'SQ-', 1, 5, NULL),
(@company_id, @branch_id, 'quote_ref', 'SQR-', 1, 5, NULL),
(@company_id, @branch_id, 'order', 'SO-', 1, 5, NULL),
(@company_id, @branch_id, 'order_ref', 'SOR-', 1, 5, NULL),
(@company_id, @branch_id, 'invoice', 'INV-', 1, 5, NULL),
(@company_id, @branch_id, 'invoice_ref', 'INVR-', 1, 5, NULL);
