-- 001_gais_seed.sql
-- GAIS (Get Information About Schools) reference tables
-- Source: Edubase open data, https://www.get-information-schools.service.gov.uk/
-- Apply once to the Supabase Postgres instance via the SQL editor.

-- ─── Local Authorities ───────────────────────────────────────────────────────

CREATE TABLE IF NOT EXISTS gais_local_authorities (
    code        TEXT PRIMARY KEY,
    name        TEXT NOT NULL,
    created_at  TIMESTAMPTZ DEFAULT now()
);

-- ─── Schools ─────────────────────────────────────────────────────────────────

CREATE TABLE IF NOT EXISTS gais_schools (
    urn                 TEXT PRIMARY KEY,
    name                TEXT NOT NULL,
    status              TEXT,        -- Open | Closed | Proposed to Open
    phase               TEXT,        -- Primary | Secondary | 16 plus | etc.
    type                TEXT,        -- Voluntary aided school | Academy | etc.
    type_group          TEXT,        -- Local authority maintained | Independent | etc.
    street              TEXT,
    locality            TEXT,
    town                TEXT,
    county              TEXT,
    postcode            TEXT,
    website             TEXT,
    telephone           TEXT,
    head_title          TEXT,
    head_first_name     TEXT,
    head_last_name      TEXT,
    la_code             TEXT REFERENCES gais_local_authorities(code),
    la_name             TEXT,
    number_of_pupils    INTEGER,
    open_date           DATE,
    close_date          DATE,
    gender              TEXT,        -- Mixed | Girls | Boys
    religious_character TEXT,
    region              TEXT,        -- Government Office Region
    created_at          TIMESTAMPTZ DEFAULT now()
);

-- ─── Indexes ─────────────────────────────────────────────────────────────────

-- Full-text search on name + town + postcode
CREATE INDEX IF NOT EXISTS gais_schools_name_fts
    ON gais_schools USING gin(to_tsvector('english', coalesce(name, '') || ' ' || coalesce(town, '') || ' ' || coalesce(postcode, '')));

-- Trigram index for ILIKE search (pg_trgm extension required)
CREATE EXTENSION IF NOT EXISTS pg_trgm;
CREATE INDEX IF NOT EXISTS gais_schools_name_trgm  ON gais_schools USING gin(name  gin_trgm_ops);
CREATE INDEX IF NOT EXISTS gais_schools_town_trgm  ON gais_schools USING gin(town  gin_trgm_ops);
CREATE INDEX IF NOT EXISTS gais_schools_postcode_trgm ON gais_schools USING gin(postcode gin_trgm_ops);

-- Status and LA for filtered queries
CREATE INDEX IF NOT EXISTS gais_schools_status  ON gais_schools(status);
CREATE INDEX IF NOT EXISTS gais_schools_la_code ON gais_schools(la_code);

-- ─── RLS ─────────────────────────────────────────────────────────────────────
-- Public read (these are open-data reference tables).
-- Writes are only via service-role (admin imports / seed scripts).

ALTER TABLE gais_local_authorities ENABLE ROW LEVEL SECURITY;
ALTER TABLE gais_schools           ENABLE ROW LEVEL SECURITY;

CREATE POLICY "Public read gais_local_authorities"
    ON gais_local_authorities FOR SELECT USING (true);

CREATE POLICY "Public read gais_schools"
    ON gais_schools FOR SELECT USING (true);
