-- test/fixtures/test-schema.sql
--
-- Minimum subset of the production schema required by live.integration.test.ts.
-- This is intentionally a simplified version — NOT the full Directus-managed schema.
--
-- Maintenance note: keep in sync with the real schema when column types change on
-- these tables. Specifically: entries.event_id, entry_devices.device_id (Phase 1
-- uses IMEI text; Phase 2 introduces UUID-based devices table).
--
-- Phase 1 deviation: entry_devices.device_id is TEXT (IMEI) here, matching
-- positions.device_id. The real Directus schema uses a UUID FK to devices.id.
-- The integration test uses the real queries from device-event-map.ts and
-- snapshot.ts, so this simplified schema must satisfy those joins.

-- events — the container for entries
-- The Processor reads events.id (used in snapshot WHERE e.event_id = $1).
CREATE TABLE IF NOT EXISTS events (
  id   uuid PRIMARY KEY DEFAULT gen_random_uuid()
  -- Real schema also has: organization_id FK, name, slug, discipline, starts_at, ends_at.
  -- Only columns the Processor queries are included here.
);

-- entries — race entries belonging to an event
-- The Processor reads entries.id and entries.event_id.
CREATE TABLE IF NOT EXISTS entries (
  id       uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  event_id uuid NOT NULL REFERENCES events (id) ON DELETE CASCADE
  -- Real schema also has: vehicle_id, class_id, number, etc.
);

-- entry_devices — maps a device (IMEI) to an entry.
-- Phase 1: device_id is IMEI text, matching positions.device_id.
-- Real schema: device_id is UUID FK to devices.id, joined via devices.imei.
-- This simplified form is intentional for the integration test fixture.
CREATE TABLE IF NOT EXISTS entry_devices (
  id         uuid PRIMARY KEY DEFAULT gen_random_uuid(),
  entry_id   uuid NOT NULL REFERENCES entries (id) ON DELETE CASCADE,
  device_id  text NOT NULL  -- IMEI in Phase 1
);
