-- Local-only demo seed: make demo@scriba.ai the owner of a company and mark a
-- couple of conversions as completed in the current month so the Invoices page
-- shows real line items. Reversible (see bottom for teardown).
--
-- Run with:
--   psql -h localhost -p 5432 -d scriba -f scripts/seed-demo-company.sql

BEGIN;

INSERT INTO companies (id, name, package, max_users, owner_id, license_credits_remaining, financial_data)
VALUES (
  'comp-demo',
  'Demo Bank S.p.A.',
  'professional',
  25,
  '860eb995-a62d-40a0-a4c0-81912f3b79a1',
  10000000,
  '{"billingEmail":"billing@demo.scriba.ai","billingAddress":"Via Demo 1, 20100 Milano (MI)","vatNumber":"IT01234567890","taxId":"DMOBNK00A01F205X"}'::jsonb
)
ON CONFLICT (id) DO UPDATE SET
  package = EXCLUDED.package,
  owner_id = EXCLUDED.owner_id,
  license_credits_remaining = EXCLUDED.license_credits_remaining,
  financial_data = EXCLUDED.financial_data,
  suspended_at = NULL,
  suspended_reason = NULL;

UPDATE "user"
SET company_id = 'comp-demo', is_owner = true, tier = 'professional'
WHERE id = '860eb995-a62d-40a0-a4c0-81912f3b79a1';

-- Two completed conversions this month. The annual license credit (€100k) covers
-- both in full, so net billable is €0 and the credit pool is decremented.
-- gross €11,568 = 1,156,800 tokens ; gross €9,640 = 964,000 tokens.
UPDATE projects
SET status = 'completed', completed_at = now(),
    approved_estimated_tokens = 1156800, approved_gross_eur_cents = 1156800,
    approved_credits_tokens_applied = 1156800, approved_net_eur_cents = 0
WHERE id = 'proj-secb-complete';

UPDATE projects
SET status = 'completed', completed_at = now(),
    approved_estimated_tokens = 964000, approved_gross_eur_cents = 964000,
    approved_credits_tokens_applied = 964000, approved_net_eur_cents = 0
WHERE id = 'proj-demo-full';

-- Decrement the annual credit pool by the tokens applied above
-- (10,000,000 - 1,156,800 - 964,000 = 7,879,200 → €78,792 remaining, €21,208 used).
UPDATE companies SET license_credits_remaining = 7879200 WHERE id = 'comp-demo';

COMMIT;

SELECT id, name, status, approved_net_eur_cents, completed_at
FROM projects
WHERE user_id = '860eb995-a62d-40a0-a4c0-81912f3b79a1' AND status = 'completed';

SELECT id, name, package, owner_id FROM companies WHERE id = 'comp-demo';

-- ── Teardown (run manually to revert) ───────────────────────────────────────
-- UPDATE "user" SET company_id = NULL, is_owner = false, tier = 'starter'
--   WHERE id = '860eb995-a62d-40a0-a4c0-81912f3b79a1';
-- DELETE FROM companies WHERE id = 'comp-demo';
