-- Organisational hierarchy (per ADR-0027).
--
-- Region → Delegation → Structure. The portal's ADR-0025 scope-axis
-- dereferences these three layers — the BFF guard
-- `principalCoversResource` walks etablissement → delegation → region
-- to decide whether a scope covers a resource. The schema lives in
-- the public schema; ADR-0029 will populate the full APF inventory
-- via the cascade sync, additively into the columns defined here.
--
-- Inline seed at the bottom of this file: just the codes the
-- test tenant exercises (Région Nouvelle-Aquitaine, Délégation
-- Gironde, two médico-social structures + the APF national siège).
-- Superseded — not extended — by ADR-0029's cascade sync once it
-- ships.

-- CreateTable
CREATE TABLE "regions" (
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,

    CONSTRAINT "regions_pkey" PRIMARY KEY ("code")
);

-- CreateTable
CREATE TABLE "delegations" (
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "region_code" TEXT NOT NULL,

    CONSTRAINT "delegations_pkey" PRIMARY KEY ("code")
);

-- Closed-set CHECK on Structure.kind. Mirrors STRUCTURE_KINDS in
-- apps/portal-bff/src/structures/structure-kind.ts. The CI drift gate
-- (scripts/check-catalogue-drift.mjs) asserts the TS constant matches
-- the values used in code; this CHECK constraint provides the
-- equivalent guarantee at the DB layer for any code path that
-- bypasses the type system.
CREATE TABLE "structures" (
    "code" TEXT NOT NULL,
    "name" TEXT NOT NULL,
    "kind" TEXT NOT NULL,
    "finess" TEXT,
    "siret" TEXT,
    "code_paie" TEXT,
    "delegation_code" TEXT,

    CONSTRAINT "structures_pkey" PRIMARY KEY ("code"),
    CONSTRAINT "structures_kind_check" CHECK (
      "kind" IN (
        'medico_social',
        'antenne',
        'dispositif',
        'entreprise_adaptee',
        'mouvement',
        'administratif',
        'siege'
      )
    )
);

-- CreateIndex
CREATE INDEX "delegations_region_code_idx" ON "delegations"("region_code");

-- CreateIndex
CREATE UNIQUE INDEX "structures_finess_key" ON "structures"("finess");

-- CreateIndex
CREATE UNIQUE INDEX "structures_siret_key" ON "structures"("siret");

-- CreateIndex
CREATE UNIQUE INDEX "structures_code_paie_key" ON "structures"("code_paie");

-- CreateIndex
CREATE INDEX "structures_kind_idx" ON "structures"("kind");

-- CreateIndex
CREATE INDEX "structures_delegation_code_idx" ON "structures"("delegation_code");

-- AddForeignKey
ALTER TABLE "delegations" ADD CONSTRAINT "delegations_region_code_fkey"
    FOREIGN KEY ("region_code") REFERENCES "regions"("code")
    ON DELETE RESTRICT ON UPDATE CASCADE;

-- AddForeignKey
-- ON DELETE SET NULL because delegationCode is optional in the Prisma
-- schema (Structure.delegation Delegation?). Matches Prisma's default
-- for nullable relations; the drift gate would otherwise flag this
-- on every `prisma migrate dev`.
ALTER TABLE "structures" ADD CONSTRAINT "structures_delegation_code_fkey"
    FOREIGN KEY ("delegation_code") REFERENCES "delegations"("code")
    ON DELETE SET NULL ON UPDATE CASCADE;

-- ------------------------------------------------------------------
-- Test-tenant reference seed (per ADR-0027 §"Seeding posture").
--
-- Region Nouvelle-Aquitaine + Délégation Gironde + the structures
-- referenced by the 19 personas in notes/test-tenant-role-assignments.md:
--   - 0330800013 (APF Bordeaux, FINESS = code)
--   - 0330800021 (Complexe Mérignac, FINESS = code)
--   - 'siege' (APF national headquarters)
--
-- Cascade-sync (ADR-0029) supersedes this seed entirely when it
-- ships — the cleanup migration responsible for that truncates and
-- repopulates from cascade's authoritative inventory.
-- ------------------------------------------------------------------

INSERT INTO "regions" ("code", "name") VALUES
    ('75', 'Nouvelle-Aquitaine');

INSERT INTO "delegations" ("code", "name", "region_code") VALUES
    ('33', 'Gironde', '75');

INSERT INTO "structures" ("code", "name", "kind", "finess", "delegation_code") VALUES
    ('0330800013', 'APF Bordeaux', 'medico_social', '0330800013', '33'),
    ('0330800021', 'Complexe Mérignac', 'medico_social', '0330800021', '33');

-- Siège has no delegation parent and no FINESS/SIRET/codePaie (those
-- columns stay NULL — the unique indexes tolerate multiple NULLs).
INSERT INTO "structures" ("code", "name", "kind") VALUES
    ('siege', 'Siège APF France handicap', 'siege');
