-- mockshot-nuke.sql — exact reversal of mockshot-seed.sql, FK order.
--
-- The seed owns three predicates and nothing else: organizations whose `key`
-- starts with `mockshot_`, users whose OIDC subject starts with `mockshot:`,
-- and the catalog rows keyed `mockshot_hosting` (entitlement set) and
-- `mockshot_*` (products, and the prices hanging off them). Every other row
-- the seed created is reached from one of those three: through its
-- organization (pools, workspaces, assignments, accounts, subscriptions,
-- items, invoices, payments, grants, provisions) or through its catalog
-- parent. There is no marker column, no owner email predicate, and no name
-- match anywhere in this file.
--
--   psql -h localhost -p $POSTGRES_PORT -U member_console -d member_console \
--     -v ON_ERROR_STOP=1 -f test/mockshot-nuke.sql

BEGIN;

-- Provisions first: they reference pools, grants, subscriptions, products,
-- and the entitlement set, so every later delete depends on them being gone.
DELETE FROM core.pool_provisions
WHERE pool_id IN (
    SELECT rp.pool_id FROM core.resource_pools rp
    JOIN core.organizations o ON o.org_id = rp.org_id
    WHERE o.key LIKE 'mockshot\_%'
);

DELETE FROM core.grants
WHERE granted_to_org_id IN (SELECT org_id FROM core.organizations WHERE key LIKE 'mockshot\_%');

DELETE FROM core.payments
WHERE billing_account_id IN (
    SELECT a.billing_account_id FROM core.accounts a
    JOIN core.organizations o ON o.org_id = a.org_id
    WHERE o.key LIKE 'mockshot\_%'
);

DELETE FROM core.invoices
WHERE billing_account_id IN (
    SELECT a.billing_account_id FROM core.accounts a
    JOIN core.organizations o ON o.org_id = a.org_id
    WHERE o.key LIKE 'mockshot\_%'
);

-- Items reference prices, so they go before the catalog below.
DELETE FROM core.subscription_items
WHERE subscription_id IN (
    SELECT s.subscription_id FROM core.subscriptions s
    JOIN core.accounts a ON a.billing_account_id = s.billing_account_id
    JOIN core.organizations o ON o.org_id = a.org_id
    WHERE o.key LIKE 'mockshot\_%'
);

DELETE FROM core.subscriptions
WHERE billing_account_id IN (
    SELECT a.billing_account_id FROM core.accounts a
    JOIN core.organizations o ON o.org_id = a.org_id
    WHERE o.key LIKE 'mockshot\_%'
);

DELETE FROM core.accounts
WHERE org_id IN (SELECT org_id FROM core.organizations WHERE key LIKE 'mockshot\_%');

-- Workspaces and pools: the assignment joins them, so it goes first.
DELETE FROM core.pool_assignments
WHERE pool_id IN (
    SELECT rp.pool_id FROM core.resource_pools rp
    JOIN core.organizations o ON o.org_id = rp.org_id
    WHERE o.key LIKE 'mockshot\_%'
);

DELETE FROM core.workspaces
WHERE org_id IN (SELECT org_id FROM core.organizations WHERE key LIKE 'mockshot\_%');

DELETE FROM core.resource_pools
WHERE org_id IN (SELECT org_id FROM core.organizations WHERE key LIKE 'mockshot\_%');

-- After the grants, accounts, and pools that referenced the organizations.
DELETE FROM core.organizations WHERE key LIKE 'mockshot\_%';

-- The catalog, after the subscription items and provisions that referenced
-- it: prices before their products, the rules before their set.
DELETE FROM core.prices
WHERE product_id IN (SELECT product_id FROM core.products WHERE key LIKE 'mockshot\_%');

DELETE FROM core.products WHERE key LIKE 'mockshot\_%';

DELETE FROM core.entitlement_set_rules
WHERE set_id IN (SELECT set_id FROM core.entitlement_sets WHERE key = 'mockshot_hosting');

DELETE FROM core.entitlement_sets WHERE key = 'mockshot_hosting';

-- The team org type is vocabulary keyed on the type itself, so it is removed
-- only once nothing is left using it — a dependency check, not a marker.
DELETE FROM core.org_types t
WHERE t.org_type = 'team'
  AND NOT EXISTS (SELECT 1 FROM core.organizations o WHERE o.org_type = t.org_type);

-- Cascades the mock persons (fk_persons_user_id ON DELETE CASCADE).
DELETE FROM core.users WHERE oidc_subject LIKE 'mockshot:%';

COMMIT;

-- Should all be zero
SELECT (SELECT count(*) FROM core.users WHERE oidc_subject LIKE 'mockshot:%')          AS users_left,
       (SELECT count(*) FROM core.organizations
          WHERE key LIKE 'mockshot\_%' AND key NOT LIKE 'mockshot\_t%')                AS personal_orgs_left,
       (SELECT count(*) FROM core.organizations WHERE key LIKE 'mockshot\_t%')         AS team_orgs_left,
       (SELECT count(*) FROM core.grants g JOIN core.organizations o
          ON o.org_id = g.granted_to_org_id WHERE o.key LIKE 'mockshot\_%')            AS grants_left,
       (SELECT count(*) FROM core.accounts a JOIN core.organizations o
          ON o.org_id = a.org_id WHERE o.key LIKE 'mockshot\_%')                       AS accounts_left,
       (SELECT count(*) FROM core.products WHERE key LIKE 'mockshot\_%')               AS products_left,
       (SELECT count(*) FROM core.prices p JOIN core.products pd
          ON pd.product_id = p.product_id WHERE pd.key LIKE 'mockshot\_%')             AS prices_left,
       (SELECT count(*) FROM core.entitlement_sets WHERE key = 'mockshot_hosting')     AS entitlement_sets_left;
