-- ═══════════════════════════════════════════════════════════════════════════
-- Seed a minimal chart_of_accounts (5-account starter) + budget categories.
-- These are the accounts the revenue journal (Dr 1121 / Cr 4110 / Cr 8110)
-- posts against once the Goods Invoice ack flow lands.
-- ═══════════════════════════════════════════════════════════════════════════

INSERT INTO chart_of_accounts (account_code, account_name, account_type, is_control) VALUES
  ('1000', 'ASSETS',                'ASSET',     1),
  ('1100', 'Current Assets',        'ASSET',     1),
  ('1121', 'Trade Receivables',     'ASSET',     0),
  ('1200', 'Bank & Cash',           'ASSET',     1),
  ('2000', 'LIABILITIES',           'LIABILITY', 1),
  ('2100', 'Trade Payables',        'LIABILITY', 0),
  ('2200', 'VAT Payable',           'LIABILITY', 0),
  ('3000', 'EQUITY',                'EQUITY',    1),
  ('4000', 'REVENUE',               'REVENUE',   1),
  ('4110', 'Goods Sales Revenue',   'REVENUE',   0),
  ('4120', 'Services Revenue',      'REVENUE',   0),
  ('4130', 'Works Revenue',         'REVENUE',   0),
  ('5000', 'COST OF SALES',         'EXPENSE',   1),
  ('5110', 'Goods Cost of Sales',   'EXPENSE',   0),
  ('6000', 'OPERATING EXPENSES',    'EXPENSE',   1),
  ('8000', 'TAX ACCOUNTS',          'LIABILITY', 1),
  ('8110', 'Output VAT',            'LIABILITY', 0);

INSERT INTO budget_item_categories (category_name, category_code, description) VALUES
  ('Materials',       'MAT', 'Consumable materials procured for a project'),
  ('Manpower',        'MAN', 'Labour costs — wages, allowances, per-diems'),
  ('Equipment',       'EQP', 'Hired or company-owned equipment costs'),
  ('Tools',           'TOL', 'Small tools & consumables'),
  ('Mobilisation',    'MOB', 'Site mobilisation and demobilisation costs'),
  ('Transport',       'TRP', 'Freight, deliveries, site transport'),
  ('Subcontract',     'SUB', 'Subcontractor costs'),
  ('Overheads',       'OVH', 'Project overheads not attributable to a line'),
  ('Contingency',     'CNT', 'Contingency reserve released via CRV');

-- 9 standard checklist keys for CRM Project Onboarding ---------------------

INSERT INTO controllers_actions (controller_name, action_name) VALUES
  ('Login', 'index'), ('Logout', 'index'), ('Dashboard', 'index'),
  ('Customers', 'index'), ('Customers', 'add'), ('Customers', 'save'), ('Customers', 'view'),
  ('Projects', 'index'), ('Projects', 'add'), ('Projects', 'save'), ('Projects', 'view'),
  ('Onboarding', 'index'), ('Onboarding', 'wizard'), ('Onboarding', 'checklist'),
  ('Onboarding', 'toggleAj'), ('Onboarding', 'submitAj'),
  ('FinChains', 'myQueue'), ('FinChains', 'inFlight'), ('FinChains', 'definitions'),
  ('FinChains', 'approveAj'), ('FinChains', 'sendBackAj'), ('FinChains', 'declineAj'),
  ('FinChains', 'view');

-- Grant CRM/PC/Executive/Administrator perms on the new controllers --------

INSERT INTO user_groups_perms (user_group_id, controller_allowed, action_allowed)
SELECT ug.c0d, x.controller_allowed, x.action_allowed
  FROM user_groups ug
  CROSS JOIN (
    -- CRM's slate: customers, projects, onboarding, approvals
    SELECT 'Customers'  AS controller_allowed, 'index' AS action_allowed UNION
    SELECT 'Customers','add'   UNION SELECT 'Customers','save' UNION SELECT 'Customers','view' UNION
    SELECT 'Projects','index'  UNION SELECT 'Projects','add'   UNION SELECT 'Projects','save'  UNION SELECT 'Projects','view' UNION
    SELECT 'Onboarding','index'   UNION SELECT 'Onboarding','wizard'    UNION SELECT 'Onboarding','checklist' UNION
    SELECT 'Onboarding','toggleAj' UNION SELECT 'Onboarding','submitAj' UNION
    SELECT 'FinChains','myQueue' UNION SELECT 'FinChains','view'    UNION SELECT 'FinChains','approveAj' UNION
    SELECT 'FinChains','sendBackAj' UNION SELECT 'FinChains','declineAj'
  ) x
 WHERE ug.user_group_name IN ('CRM', 'Projects Coordinator', 'Executive', 'Administrator')
ON DUPLICATE KEY UPDATE user_group_id = user_group_id;

-- PM, FM, Accountant: read-only on Projects + Customers + approval queue --
INSERT INTO user_groups_perms (user_group_id, controller_allowed, action_allowed)
SELECT ug.c0d, x.controller_allowed, x.action_allowed
  FROM user_groups ug
  CROSS JOIN (
    SELECT 'Customers'  AS controller_allowed, 'index' AS action_allowed UNION
    SELECT 'Customers','view' UNION
    SELECT 'Projects','index' UNION SELECT 'Projects','view' UNION
    SELECT 'FinChains','myQueue' UNION SELECT 'FinChains','view' UNION SELECT 'FinChains','approveAj' UNION
    SELECT 'FinChains','sendBackAj' UNION SELECT 'FinChains','declineAj'
  ) x
 WHERE ug.user_group_name IN ('Project Manager', 'Finance Manager', 'Accountant')
ON DUPLICATE KEY UPDATE user_group_id = user_group_id;

-- Field Supervisor + Procurement + Stores: read-only on Projects ---------
INSERT INTO user_groups_perms (user_group_id, controller_allowed, action_allowed)
SELECT ug.c0d, 'Projects', 'index' FROM user_groups ug WHERE ug.user_group_name IN ('Field Supervisor','Procurement','Stores')
ON DUPLICATE KEY UPDATE user_group_id = user_group_id;
