-- New cost-of-sales and operating expense leaf accounts for common studio spending
INSERT INTO public.accounts (code, name, account_type_id, parent_id, description)
SELECT v.code, v.name,
       (SELECT id FROM public.account_types WHERE code = v.tcode),
       (SELECT id FROM public.accounts WHERE code = v.parent),
       v.descr
FROM (VALUES
  ('5050','Model & Talent Fees','COS','5000','Fees paid to models, actors and talent for shoots'),
  ('5060','Makeup, Hair & Styling','COS','5000','Makeup artists, hair and styling costs per shoot'),
  ('5070','Wardrobe & Costume Rental','COS','5000','Wardrobe, costume and accessory rental for productions'),
  ('5080','Location & Permit Fees','COS','5000','Outside location hire and shooting permits'),
  ('6310','Telephone & Mobile','EXPENSE','6300','Mobile bills, landline and call packages'),
  ('6360','Cloud Storage & Backup','EXPENSE','6300','Cloud storage, backup and archive subscriptions'),
  ('6510','Cleaning & Janitorial','EXPENSE','6500','Studio cleaning, janitorial supplies and services'),
  ('6520','Security Services','EXPENSE','6500','Guards, alarm monitoring and security contracts'),
  ('6780','Courier & Postage','EXPENSE','6700','Courier, delivery and postage charges'),
  ('6790','Licences, Permits & Govt Fees','EXPENSE','6700','Trade licence, renewals and government fees'),
  ('6800','Uniforms & Staff Welfare','EXPENSE','6700','Staff uniforms, welfare and refreshment for the team'),
  ('6810','Bad Debt Expense','EXPENSE','6700','Customer balances written off as uncollectible'),
  ('6820','Donations & CSR','EXPENSE','6700','Donations, sponsorships and community contributions')
) AS v(code, name, tcode, parent, descr)
WHERE NOT EXISTS (SELECT 1 FROM public.accounts a WHERE a.code = v.code);

-- Every active expense leaf account gets a matching category so expenses auto-post correctly
INSERT INTO public.expense_categories (name, account_id, is_active)
SELECT a.name, a.id, true
FROM public.accounts a
JOIN public.account_types t ON t.id = a.account_type_id
WHERE t.code IN ('COS','EXPENSE')
  AND a.is_active
  AND NOT EXISTS (SELECT 1 FROM public.accounts c WHERE c.parent_id = a.id)
  AND NOT EXISTS (SELECT 1 FROM public.expense_categories ec WHERE ec.account_id = a.id);