// @generated by `cargo run --manifest-path rust/Cargo.toml -p syncular-codegen --`
// Source: migrations/*.sql

use syncular_runtime::app_schema::current_schema_version as latest_schema_version;
pub use syncular_runtime::app_schema::{checksum, split_sql_statements, EmbeddedMigration};

pub const MIGRATIONS: &[EmbeddedMigration] = &[
    EmbeddedMigration {
        version: "0001",
        schema_version: 1,
        name: "initial",
        up_sql: "CREATE TABLE IF NOT EXISTS projects (\n  id TEXT PRIMARY KEY,\n  name TEXT NOT NULL,\n  owner_id TEXT NOT NULL,\n  archived INTEGER NOT NULL DEFAULT 0,\n  server_version BIGINT NOT NULL DEFAULT 0\n) WITHOUT ROWID;\n\nCREATE TABLE IF NOT EXISTS tasks (\n  id TEXT PRIMARY KEY,\n  title TEXT NOT NULL,\n  completed INTEGER NOT NULL DEFAULT 0,\n  user_id TEXT NOT NULL,\n  project_id TEXT NULL,\n  server_version BIGINT NOT NULL DEFAULT 0,\n  image TEXT NULL,\n  title_yjs_state TEXT NULL\n) WITHOUT ROWID;\n\nCREATE TABLE IF NOT EXISTS comments (\n  id TEXT PRIMARY KEY,\n  task_id TEXT NOT NULL,\n  project_id TEXT NULL,\n  body TEXT NOT NULL,\n  author_id TEXT NOT NULL,\n  deleted INTEGER NOT NULL DEFAULT 0,\n  server_version BIGINT NOT NULL DEFAULT 0\n) WITHOUT ROWID;",
    },
    EmbeddedMigration {
        version: "0002",
        schema_version: 2,
        name: "blob_client_tables",
        up_sql: "",
    },
    EmbeddedMigration {
        version: "0003",
        schema_version: 3,
        name: "retry_backoff",
        up_sql: "",
    },
    EmbeddedMigration {
        version: "0004",
        schema_version: 4,
        name: "encrypted_crdt_tables",
        up_sql: "",
    },
    EmbeddedMigration {
        version: "0005",
        schema_version: 5,
        name: "encrypted_crdt_server_seq",
        up_sql: "",
    },
    EmbeddedMigration {
        version: "0006",
        schema_version: 6,
        name: "crdt_document_persistence",
        up_sql: "",
    },
    EmbeddedMigration {
        version: "0007",
        schema_version: 7,
        name: "verified_roots",
        up_sql: "",
    },
    EmbeddedMigration {
        version: "0008",
        schema_version: 8,
        name: "add_task_descriptions",
        up_sql: "ALTER TABLE tasks ADD COLUMN description TEXT NULL;\n\nCREATE TABLE IF NOT EXISTS local_preferences (\n  id TEXT PRIMARY KEY,\n  key TEXT NOT NULL,\n  value TEXT NOT NULL\n);",
    },
];

pub const LOCAL_BASE_TABLE_SETUP_SQL: &[&str] = &[
    "CREATE TABLE IF NOT EXISTS \"comments\" (\n  \"id\" TEXT PRIMARY KEY,\n  \"task_id\" TEXT NOT NULL,\n  \"project_id\" TEXT,\n  \"body\" TEXT NOT NULL,\n  \"author_id\" TEXT NOT NULL,\n  \"deleted\" INTEGER NOT NULL DEFAULT 0,\n  \"server_version\" INTEGER NOT NULL DEFAULT 0\n) WITHOUT ROWID",
    "CREATE TABLE IF NOT EXISTS \"projects\" (\n  \"id\" TEXT PRIMARY KEY,\n  \"name\" TEXT NOT NULL,\n  \"owner_id\" TEXT NOT NULL,\n  \"archived\" INTEGER NOT NULL DEFAULT 0,\n  \"server_version\" INTEGER NOT NULL DEFAULT 0\n) WITHOUT ROWID",
    "CREATE TABLE IF NOT EXISTS \"tasks\" (\n  \"id\" TEXT PRIMARY KEY,\n  \"title\" TEXT NOT NULL,\n  \"completed\" INTEGER NOT NULL DEFAULT 0,\n  \"user_id\" TEXT NOT NULL,\n  \"project_id\" TEXT,\n  \"server_version\" INTEGER NOT NULL DEFAULT 0,\n  \"image\" TEXT,\n  \"title_yjs_state\" TEXT,\n  \"description\" TEXT\n) WITHOUT ROWID",
];

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LocalReadModelSql {
    pub name: &'static str,
    pub output_table: &'static str,
    pub setup_sql: &'static [&'static str],
    pub rebuild_sql: &'static [&'static str],
}

pub const TASKCOUNTSBYUSERCOMPLETION_SETUP_SQL: &[&str] = &[
    "CREATE TABLE IF NOT EXISTS \"syncular_task_counts\" (\n  \"user_id\" TEXT NOT NULL,\n  \"completed\" INTEGER NOT NULL,\n  \"task_count\" INTEGER NOT NULL DEFAULT 0,\n  PRIMARY KEY (\"user_id\", \"completed\")\n) WITHOUT ROWID",
    "CREATE TRIGGER IF NOT EXISTS \"syncular_rm_taskCountsByUserCompletion_insert\"\nAFTER INSERT ON \"tasks\"\nBEGIN\n  INSERT INTO \"syncular_task_counts\" (\"user_id\", \"completed\", \"task_count\")\n  VALUES (new.\"user_id\", new.\"completed\", 1)\n  ON CONFLICT(\"user_id\", \"completed\") DO UPDATE SET\n    \"task_count\" = \"task_count\" + 1;\nEND",
    "CREATE TRIGGER IF NOT EXISTS \"syncular_rm_taskCountsByUserCompletion_delete\"\nAFTER DELETE ON \"tasks\"\nBEGIN\n  UPDATE \"syncular_task_counts\"\n  SET \"task_count\" = \"task_count\" - 1\n  WHERE \"user_id\" = old.\"user_id\"\n          AND \"completed\" = old.\"completed\";\n  DELETE FROM \"syncular_task_counts\" WHERE \"task_count\" <= 0;\nEND",
    "CREATE TRIGGER IF NOT EXISTS \"syncular_rm_taskCountsByUserCompletion_update_group\"\nAFTER UPDATE OF \"user_id\", \"completed\" ON \"tasks\"\nWHEN old.\"user_id\" IS NOT new.\"user_id\"\n        OR old.\"completed\" IS NOT new.\"completed\"\nBEGIN\n  UPDATE \"syncular_task_counts\"\n  SET \"task_count\" = \"task_count\" - 1\n  WHERE \"user_id\" = old.\"user_id\"\n          AND \"completed\" = old.\"completed\";\n  DELETE FROM \"syncular_task_counts\" WHERE \"task_count\" <= 0;\n  INSERT INTO \"syncular_task_counts\" (\"user_id\", \"completed\", \"task_count\")\n  VALUES (new.\"user_id\", new.\"completed\", 1)\n  ON CONFLICT(\"user_id\", \"completed\") DO UPDATE SET\n    \"task_count\" = \"task_count\" + 1;\nEND",
];

pub const TASKCOUNTSBYUSERCOMPLETION_REBUILD_SQL: &[&str] = &[
    "DELETE FROM \"syncular_task_counts\"",
    "INSERT INTO \"syncular_task_counts\" (\"user_id\", \"completed\", \"task_count\")\nSELECT \"user_id\", \"completed\", count(*)\nFROM \"tasks\"\nGROUP BY \"user_id\", \"completed\"",
];

pub const LOCAL_READ_MODELS: &[LocalReadModelSql] = &[LocalReadModelSql {
    name: "taskCountsByUserCompletion",
    output_table: "syncular_task_counts",
    setup_sql: TASKCOUNTSBYUSERCOMPLETION_SETUP_SQL,
    rebuild_sql: TASKCOUNTSBYUSERCOMPLETION_REBUILD_SQL,
}];

pub fn current_schema_version() -> i32 {
    latest_schema_version(MIGRATIONS)
}
