-- Multi-project playground schema (MySQL)
-- Purpose: keep many small projects in one database using project_key namespace.

SET NAMES utf8mb4;

CREATE TABLE IF NOT EXISTS projects (
  project_key VARCHAR(100) NOT NULL,
  name VARCHAR(255) NOT NULL,
  description TEXT NULL,
  meta_json LONGTEXT NULL,
  created_at DATETIME(3) NOT NULL,
  updated_at DATETIME(3) NOT NULL,
  PRIMARY KEY (project_key),
  KEY idx_projects_updated_at (updated_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS project_documents (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  project_key VARCHAR(100) NOT NULL,
  doc_type VARCHAR(100) NOT NULL,
  doc_key VARCHAR(191) NOT NULL,
  data_json LONGTEXT NOT NULL,
  meta_json LONGTEXT NULL,
  created_at DATETIME(3) NOT NULL,
  updated_at DATETIME(3) NOT NULL,
  PRIMARY KEY (id),
  UNIQUE KEY uq_project_documents_namespace (project_key, doc_type, doc_key),
  KEY idx_project_documents_project_type_updated (project_key, doc_type, updated_at),
  KEY idx_project_documents_project_updated (project_key, updated_at),
  CONSTRAINT fk_project_documents_project_key
    FOREIGN KEY (project_key) REFERENCES projects(project_key)
    ON UPDATE CASCADE
    ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS project_events (
  id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  project_key VARCHAR(100) NOT NULL,
  stream VARCHAR(100) NOT NULL,
  event_type VARCHAR(100) NOT NULL,
  payload_json LONGTEXT NOT NULL,
  observed_at DATETIME(3) NOT NULL,
  created_at DATETIME(3) NOT NULL,
  PRIMARY KEY (id),
  KEY idx_project_events_project_stream_created (project_key, stream, created_at),
  KEY idx_project_events_project_created (project_key, created_at),
  CONSTRAINT fk_project_events_project_key
    FOREIGN KEY (project_key) REFERENCES projects(project_key)
    ON UPDATE CASCADE
    ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS project_settings (
  project_key VARCHAR(100) NOT NULL,
  setting_key VARCHAR(191) NOT NULL,
  setting_value LONGTEXT NOT NULL,
  updated_at DATETIME(3) NOT NULL,
  PRIMARY KEY (project_key, setting_key),
  KEY idx_project_settings_updated_at (updated_at),
  CONSTRAINT fk_project_settings_project_key
    FOREIGN KEY (project_key) REFERENCES projects(project_key)
    ON UPDATE CASCADE
    ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Seed one project namespace for this repository (optional)
INSERT INTO projects (
  project_key,
  name,
  description,
  meta_json,
  created_at,
  updated_at
)
VALUES (
  'air-watcher',
  'Air Watcher',
  'Flight price watcher dashboard and alerts',
  JSON_OBJECT('owner', 'team', 'status', 'active'),
  UTC_TIMESTAMP(3),
  UTC_TIMESTAMP(3)
)
ON DUPLICATE KEY UPDATE
  name = VALUES(name),
  description = VALUES(description),
  meta_json = VALUES(meta_json),
  updated_at = VALUES(updated_at);

-- Mapping guide for Air-Watcher:
-- 1) watch row            -> project_documents (doc_type='watch', doc_key=watch_id)
-- 2) app settings         -> project_settings  (setting_key='global_controls' etc.)
-- 3) user profiles        -> project_settings  (setting_key='user_profiles')
-- 4) watch alert events   -> project_events    (stream='watch_events')
