-- Suite e2e database seed.
--
-- Loaded by the `db-seed` service in docker-compose.suite-e2e.yml after
-- annotations has run its own DatabaseMigrator (which creates the schema +
-- inserts the canonical detection_classes 0..18). This file therefore only
-- adds rows that the e2e scenario depends on but the production runtime does
-- NOT seed automatically.
--
-- Idempotency: every statement uses ON CONFLICT / IF NOT EXISTS so re-running
-- the seed (e.g. on a `down -v` followed by `up`) lands the same final state.
--
-- Schema reference: annotations/src/Database/DatabaseMigrator.cs.

\set ON_ERROR_STOP on

-- Wait until annotations has populated its schema. The db-seed container starts
-- only after postgres-local is healthy, but annotations may still be spinning
-- up its tables. A bounded poll keeps the seed deterministic.
DO $$
DECLARE
  attempt int := 0;
BEGIN
  WHILE attempt < 60 LOOP
    PERFORM 1
    FROM information_schema.tables
    WHERE table_schema = 'public' AND table_name = 'detection_classes';
    IF FOUND THEN
      EXIT;
    END IF;
    PERFORM pg_sleep(1);
    attempt := attempt + 1;
  END LOOP;

  IF attempt >= 60 THEN
    RAISE EXCEPTION 'detection_classes table not found after 60s — annotations migration did not complete';
  END IF;
END $$;

-- Default system_settings row. Annotations starts without one, but several
-- spec assertions rely on `silent_detection = false` and known thumbnail dims
-- so overlay rendering is reproducible.
INSERT INTO system_settings (
  id, name, military_unit,
  default_camera_width, default_camera_fov,
  thumbnail_width, thumbnail_height, thumbnail_border,
  generate_annotated_image, silent_detection
) VALUES (
  '00000000-0000-0000-0000-00000000aaaa',
  'azaion-suite-e2e',
  'e2e-unit',
  3840, 70,
  240, 135, 10,
  true, false
) ON CONFLICT (id) DO NOTHING;

-- Default directory_settings row. Annotations writes media files under the
-- paths defined here; the e2e-runner doesn't read these directly but the
-- service requires the row to exist on first hit.
INSERT INTO directory_settings (
  id, videos_dir, images_dir, labels_dir, results_dir,
  thumbnails_dir, gps_sat_dir, gps_route_dir
) VALUES (
  '00000000-0000-0000-0000-00000000bbbb',
  '/data/videos', '/data/images', '/data/labels', '/data/results',
  '/data/thumbnails', '/data/gps_sat', '/data/gps_route'
) ON CONFLICT (id) DO NOTHING;

-- Default camera_settings row used by detections to size bbox-to-meters.
INSERT INTO camera_settings (
  id, altitude, focal_length, sensor_width
) VALUES (
  '00000000-0000-0000-0000-00000000cccc',
  100, 50, 36
) ON CONFLICT (id) DO NOTHING;

-- Stable e2e user. The UUID is referenced by the spec when asserting
-- annotation rows. Annotations does not own a `users` table — user identity
-- is carried in JWTs minted with JWT_SECRET; the user_id here just needs to
-- be deterministic and stable across runs.
-- Stored in user_settings so the spec can `SELECT user_id` to confirm the
-- seed ran.
INSERT INTO user_settings (
  id, user_id,
  annotations_left_panel_width, annotations_right_panel_width,
  dataset_left_panel_width,    dataset_right_panel_width
) VALUES (
  '00000000-0000-0000-0000-00000000dddd',
  '00000000-0000-0000-0000-0000e2e2e2e2',
  300, 400, 320, 320
) ON CONFLICT (id) DO NOTHING;

-- Sanity check — fail loudly if the canonical detection_classes are missing.
-- annotations/src/Database/DatabaseMigrator.cs inserts ids 0..18 unconditionally.
DO $$
DECLARE
  cnt int;
BEGIN
  SELECT COUNT(*) INTO cnt FROM detection_classes WHERE id BETWEEN 0 AND 18;
  IF cnt < 19 THEN
    RAISE EXCEPTION 'expected canonical detection_classes 0..18 (count=19), got %', cnt;
  END IF;
END $$;

\echo 'suite-e2e seed complete'
