-- config domain: pipe_config, query_config, sink_config, pipe_preset_config

-- query_config: queries within a pipe
DEFINE TABLE OVERWRITE query_config SCHEMAFULL;
DEFINE FIELD OVERWRITE origin_id  ON query_config TYPE string ASSERT $value != NONE;
DEFINE FIELD OVERWRITE name       ON query_config TYPE string ASSERT $value != NONE;
DEFINE FIELD OVERWRITE query      ON query_config TYPE string;
DEFINE FIELD OVERWRITE key_column ON query_config TYPE string;
DEFINE FIELD OVERWRITE schedule   ON query_config TYPE option<string>;
DEFINE FIELD OVERWRITE sinks      ON query_config TYPE option<array>;
DEFINE FIELD OVERWRITE transform  ON query_config TYPE option<string>;
DEFINE FIELD OVERWRITE enabled    ON query_config TYPE bool DEFAULT true;
DEFINE FIELD OVERWRITE created_at ON query_config TYPE datetime DEFAULT time::now();
DEFINE INDEX OVERWRITE idx_query_source ON query_config FIELDS origin_id, name UNIQUE;

-- pipe_config: complete pipeline definitions (origin → delta → transform → target)
DEFINE TABLE OVERWRITE pipe_config SCHEMAFULL;
DEFINE FIELD OVERWRITE name               ON pipe_config TYPE string ASSERT $value != NONE;
DEFINE FIELD OVERWRITE origin_connector   ON pipe_config TYPE string ASSERT $value != NONE;
DEFINE FIELD OVERWRITE origin_dsn         ON pipe_config TYPE string ASSERT $value != NONE;
DEFINE FIELD OVERWRITE origin_credential  ON pipe_config TYPE option<string>;
DEFINE FIELD OVERWRITE trino_url          ON pipe_config TYPE option<string>;
DEFINE FIELD OVERWRITE origin_config      ON pipe_config TYPE object FLEXIBLE;
DEFINE FIELD OVERWRITE targets            ON pipe_config TYPE array DEFAULT [];
DEFINE FIELD OVERWRITE schedule           ON pipe_config TYPE object FLEXIBLE;
DEFINE FIELD OVERWRITE delta              ON pipe_config TYPE object FLEXIBLE;
DEFINE FIELD OVERWRITE retry              ON pipe_config TYPE object FLEXIBLE;
DEFINE FIELD OVERWRITE recipe             ON pipe_config TYPE option<object> FLEXIBLE;
DEFINE FIELD OVERWRITE filters            ON pipe_config TYPE array DEFAULT [];
DEFINE FIELD OVERWRITE filters.*          ON pipe_config TYPE object FLEXIBLE;
DEFINE FIELD OVERWRITE transforms         ON pipe_config TYPE array DEFAULT [];
DEFINE FIELD OVERWRITE transforms.*       ON pipe_config TYPE object FLEXIBLE;
DEFINE FIELD OVERWRITE links              ON pipe_config TYPE array DEFAULT [];
DEFINE FIELD OVERWRITE links.*            ON pipe_config TYPE object FLEXIBLE;
DEFINE FIELD OVERWRITE enabled            ON pipe_config TYPE bool DEFAULT true;
DEFINE FIELD OVERWRITE created_at         ON pipe_config TYPE datetime DEFAULT time::now();
DEFINE FIELD OVERWRITE updated_at         ON pipe_config TYPE datetime DEFAULT time::now();
DEFINE INDEX OVERWRITE idx_pipe_name ON pipe_config FIELDS name UNIQUE;

-- pipe_preset_config: reusable UI/API defaults for future pipes
DEFINE TABLE OVERWRITE pipe_preset_config SCHEMAFULL;
DEFINE FIELD OVERWRITE name               ON pipe_preset_config TYPE string ASSERT $value != NONE;
DEFINE FIELD OVERWRITE description        ON pipe_preset_config TYPE option<string>;
DEFINE FIELD OVERWRITE spec               ON pipe_preset_config TYPE object FLEXIBLE;
DEFINE FIELD OVERWRITE created_at         ON pipe_preset_config TYPE datetime DEFAULT time::now();
DEFINE FIELD OVERWRITE updated_at         ON pipe_preset_config TYPE datetime DEFAULT time::now();
DEFINE INDEX OVERWRITE idx_pipe_preset_name ON pipe_preset_config FIELDS name UNIQUE;

-- credential: encrypted secrets referenced by origin/target
DEFINE TABLE OVERWRITE credential SCHEMAFULL;
DEFINE FIELD OVERWRITE name            ON credential TYPE string ASSERT $value != NONE;
DEFINE FIELD OVERWRITE credential_type ON credential TYPE string ASSERT $value != NONE;
DEFINE FIELD OVERWRITE encrypted       ON credential TYPE string ASSERT $value != NONE;
DEFINE FIELD OVERWRITE created_at      ON credential TYPE datetime DEFAULT time::now();
DEFINE FIELD OVERWRITE updated_at      ON credential TYPE datetime DEFAULT time::now();
DEFINE INDEX OVERWRITE idx_credential_name ON credential FIELDS name UNIQUE;

-- config_version: versioned config snapshots for rollback
DEFINE TABLE OVERWRITE config_version SCHEMAFULL;
DEFINE FIELD OVERWRITE version     ON config_version TYPE int ASSERT $value != NONE;
DEFINE FIELD OVERWRITE config_json ON config_version TYPE object FLEXIBLE;
DEFINE FIELD OVERWRITE description ON config_version TYPE string DEFAULT '';
DEFINE FIELD OVERWRITE created_at  ON config_version TYPE datetime DEFAULT time::now();
DEFINE INDEX OVERWRITE idx_config_version ON config_version FIELDS version UNIQUE;

-- sink_config: registered sinks
DEFINE TABLE OVERWRITE sink_config SCHEMAFULL;
DEFINE FIELD OVERWRITE name       ON sink_config TYPE string ASSERT $value != NONE;
DEFINE FIELD OVERWRITE sink_type  ON sink_config TYPE string ASSERT $value != NONE;
DEFINE FIELD OVERWRITE config     ON sink_config TYPE object FLEXIBLE;
DEFINE FIELD OVERWRITE enabled    ON sink_config TYPE bool DEFAULT true;
DEFINE FIELD OVERWRITE created_at ON sink_config TYPE datetime DEFAULT time::now();
DEFINE FIELD OVERWRITE updated_at ON sink_config TYPE datetime DEFAULT time::now();
DEFINE INDEX OVERWRITE idx_sink_name ON sink_config FIELDS name UNIQUE;
