use std::path::Path;

use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
use sqlx::{Row, SqlitePool};

use kon_core::error::{KonError, Result};

/// Initialise the SQLite database with connection pool and run migrations.
pub async fn init(db_path: &Path) -> Result<SqlitePool> {
    if let Some(parent) = db_path.parent() {
        std::fs::create_dir_all(parent)?;
    }

    let options = SqliteConnectOptions::new()
        .filename(db_path)
        .create_if_missing(true);

    let pool = SqlitePoolOptions::new()
        .max_connections(5)
        .connect_with(options)
        .await
        .map_err(|e| KonError::StorageError(format!("Database connect failed: {e}")))?;

    sqlx::query("PRAGMA foreign_keys = ON")
        .execute(&pool)
        .await
        .map_err(|e| KonError::StorageError(format!("foreign_keys pragma failed: {e}")))?;

    run_migrations(&pool).await?;

    Ok(pool)
}

/// Run schema migrations via the versioned migration system.
async fn run_migrations(pool: &SqlitePool) -> Result<()> {
    crate::migrations::run_migrations(pool).await
}

// --- Transcript CRUD ---

/// Parameters for inserting a transcript with full provenance.
pub struct InsertTranscriptParams<'a> {
    pub id: &'a str,
    pub text: &'a str,
    pub source: &'a str,
    pub profile_id: &'a str,
    pub title: Option<&'a str>,
    pub audio_path: Option<&'a str>,
    pub duration: f64,
    pub engine: Option<&'a str>,
    pub model_id: Option<&'a str>,
    pub inference_ms: Option<i64>,
    pub sample_rate: Option<i64>,
    pub audio_channels: Option<i64>,
    pub format_mode: Option<&'a str>,
    pub remove_fillers: bool,
    pub british_english: bool,
    pub anti_hallucination: bool,
}

pub async fn insert_transcript(
    pool: &SqlitePool,
    params: &InsertTranscriptParams<'_>,
) -> Result<()> {
    sqlx::query(
        "INSERT INTO transcripts (id, text, source, profile_id, title, audio_path, duration, engine, model_id, inference_ms, sample_rate, audio_channels, format_mode, remove_fillers, british_english, anti_hallucination)
         VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)",
    )
    .bind(params.id)
    .bind(params.text)
    .bind(params.source)
    .bind(params.profile_id)
    .bind(params.title)
    .bind(params.audio_path)
    .bind(params.duration)
    .bind(params.engine)
    .bind(params.model_id)
    .bind(params.inference_ms)
    .bind(params.sample_rate)
    .bind(params.audio_channels)
    .bind(params.format_mode)
    .bind(params.remove_fillers)
    .bind(params.british_english)
    .bind(params.anti_hallucination)
    .execute(pool)
    .await
    .map_err(|e| KonError::StorageError(format!("Insert transcript failed: {e}")))?;
    Ok(())
}

pub async fn get_transcript(pool: &SqlitePool, id: &str) -> Result<Option<TranscriptRow>> {
    let row = sqlx::query(
        "SELECT id, text, source, profile_id, title, audio_path, duration, engine, model_id, inference_ms, sample_rate, audio_channels, format_mode, remove_fillers, british_english, anti_hallucination, created_at, starred, manual_tags, template, language, segments_json FROM transcripts WHERE id = ?",
    )
    .bind(id)
    .fetch_optional(pool)
    .await
    .map_err(|e| KonError::StorageError(format!("Get transcript failed: {e}")))?;

    Ok(row.map(|r| transcript_row_from(&r)))
}

pub async fn list_transcripts(pool: &SqlitePool, limit: i64) -> Result<Vec<TranscriptRow>> {
    list_transcripts_paged(pool, limit, 0).await
}

/// Paginated transcript list. `limit` is rows-per-page, `offset` is rows to skip.
/// Used by HistoryPage to load 50 at a time and append more on scroll.
pub async fn list_transcripts_paged(
    pool: &SqlitePool,
    limit: i64,
    offset: i64,
) -> Result<Vec<TranscriptRow>> {
    let rows = sqlx::query(
        "SELECT id, text, source, profile_id, title, audio_path, duration, engine, model_id, inference_ms, sample_rate, audio_channels, format_mode, remove_fillers, british_english, anti_hallucination, created_at, starred, manual_tags, template, language, segments_json FROM transcripts ORDER BY created_at DESC LIMIT ? OFFSET ?",
    )
    .bind(limit)
    .bind(offset)
    .fetch_all(pool)
    .await
    .map_err(|e| KonError::StorageError(format!("List transcripts failed: {e}")))?;

    Ok(rows.iter().map(transcript_row_from).collect())
}

/// Total count of transcripts. Useful for displaying "showing 50 of 312" in the UI.
pub async fn count_transcripts(pool: &SqlitePool) -> Result<i64> {
    let n: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM transcripts")
        .fetch_one(pool)
        .await
        .map_err(|e| KonError::StorageError(format!("Count transcripts failed: {e}")))?;
    Ok(n)
}

/// Update mutable transcript fields. Currently `text` and `title`. Other
/// columns (engine, model, audio path) are immutable post-creation. Returns
/// the number of rows updated (0 if id not found).
///
/// This is the function HistoryPage's rename flow needed; previously the
/// rename was a UI-only state change with a TODO never wired up.
/// (architecture-review.md §13)
pub async fn update_transcript(
    pool: &SqlitePool,
    id: &str,
    text: Option<&str>,
    title: Option<&str>,
) -> Result<u64> {
    // Build the SET clause dynamically so we only update the fields the
    // caller actually changed. Two fields max so we can keep this simple
    // without a query builder.
    match (text, title) {
        (Some(t), Some(ttl)) => {
            let res = sqlx::query("UPDATE transcripts SET text = ?, title = ? WHERE id = ?")
                .bind(t)
                .bind(ttl)
                .bind(id)
                .execute(pool)
                .await
                .map_err(|e| KonError::StorageError(format!("Update transcript failed: {e}")))?;
            Ok(res.rows_affected())
        }
        (Some(t), None) => {
            let res = sqlx::query("UPDATE transcripts SET text = ? WHERE id = ?")
                .bind(t)
                .bind(id)
                .execute(pool)
                .await
                .map_err(|e| KonError::StorageError(format!("Update transcript failed: {e}")))?;
            Ok(res.rows_affected())
        }
        (None, Some(ttl)) => {
            let res = sqlx::query("UPDATE transcripts SET title = ? WHERE id = ?")
                .bind(ttl)
                .bind(id)
                .execute(pool)
                .await
                .map_err(|e| KonError::StorageError(format!("Update transcript failed: {e}")))?;
            Ok(res.rows_affected())
        }
        (None, None) => Ok(0),
    }
}

/// Patch-style update for the Task 2.5 transcripts_meta columns (starred,
/// manual_tags, template, language, segments_json). Follows the same
/// COALESCE pattern as `update_task`: each `Option::Some` overwrites,
/// each `None` preserves the existing column value. Returns the refreshed
/// row or errors if `id` does not exist after the UPDATE.
///
/// Keeping this separate from `update_transcript` (text / title) means the
/// existing command surface stays stable — the viewer and history store
/// keep calling `update_transcript` for content edits, and call this for
/// UI-metadata persistence.
pub async fn update_transcript_meta(
    pool: &SqlitePool,
    id: &str,
    starred: Option<bool>,
    manual_tags: Option<&str>,
    template: Option<&str>,
    language: Option<&str>,
    segments_json: Option<&str>,
) -> Result<TranscriptRow> {
    sqlx::query(
        "UPDATE transcripts SET \
         starred = COALESCE(?, starred), \
         manual_tags = COALESCE(?, manual_tags), \
         template = COALESCE(?, template), \
         language = COALESCE(?, language), \
         segments_json = COALESCE(?, segments_json) \
         WHERE id = ?",
    )
    .bind(starred.map(|b| b as i64))
    .bind(manual_tags)
    .bind(template)
    .bind(language)
    .bind(segments_json)
    .bind(id)
    .execute(pool)
    .await
    .map_err(|e| KonError::StorageError(format!("update_transcript_meta: {e}")))?;

    get_transcript(pool, id).await?.ok_or_else(|| {
        KonError::StorageError(format!("update_transcript_meta: transcript {id} not found"))
    })
}

pub async fn delete_transcript(pool: &SqlitePool, id: &str) -> Result<()> {
    sqlx::query("DELETE FROM transcripts WHERE id = ?")
        .bind(id)
        .execute(pool)
        .await
        .map_err(|e| KonError::StorageError(format!("Delete transcript failed: {e}")))?;
    Ok(())
}

/// Full-text search over transcripts using the FTS5 virtual table created
/// in migration v2. Returns matched transcript rows ordered by FTS rank
/// (best match first). `query` follows FTS5 syntax: bare words AND together
/// by default; quote multi-word phrases; `OR`, `NOT` operators supported.
pub async fn search_transcripts(
    pool: &SqlitePool,
    query: &str,
    limit: i64,
) -> Result<Vec<TranscriptRow>> {
    let rows = sqlx::query(
        "SELECT t.id, t.text, t.source, t.profile_id, t.title, t.audio_path, t.duration, t.engine, t.model_id, t.inference_ms, t.sample_rate, t.audio_channels, t.format_mode, t.remove_fillers, t.british_english, t.anti_hallucination, t.created_at, t.starred, t.manual_tags, t.template, t.language, t.segments_json \
         FROM transcripts t \
         JOIN transcripts_fts fts ON fts.rowid = t.rowid \
         WHERE transcripts_fts MATCH ? \
         ORDER BY fts.rank LIMIT ?",
    )
    .bind(query)
    .bind(limit)
    .fetch_all(pool)
    .await
    .map_err(|e| KonError::StorageError(format!("FTS search failed: {e}")))?;

    Ok(rows.iter().map(transcript_row_from).collect())
}

// --- Task CRUD ---

/// Insert a task. `list_id` and `effort` are nullable (schema predates their
/// UI surfacing); `notes` defaults to '' at the column level. Callers that
/// want to set metadata at creation time pass `Some(...)`; omit for defaults.
pub async fn insert_task(
    pool: &SqlitePool,
    id: &str,
    text: &str,
    bucket: &str,
    source_transcript_id: Option<&str>,
    list_id: Option<&str>,
    effort: Option<&str>,
) -> Result<()> {
    sqlx::query(
        "INSERT INTO tasks (id, text, bucket, source_transcript_id, list_id, effort) \
         VALUES (?, ?, ?, ?, ?, ?)",
    )
    .bind(id)
    .bind(text)
    .bind(bucket)
    .bind(source_transcript_id)
    .bind(list_id)
    .bind(effort)
    .execute(pool)
    .await
    .map_err(|e| KonError::StorageError(format!("Insert task failed: {e}")))?;
    Ok(())
}

pub async fn list_tasks(pool: &SqlitePool) -> Result<Vec<TaskRow>> {
    let rows = sqlx::query(
        "SELECT id, text, bucket, list_id, effort, notes, done, done_at, created_at, \
         source_transcript_id, parent_task_id \
         FROM tasks WHERE parent_task_id IS NULL ORDER BY created_at DESC",
    )
    .fetch_all(pool)
    .await
    .map_err(|e| KonError::StorageError(format!("List tasks failed: {e}")))?;

    Ok(rows.into_iter().map(task_row_from).collect())
}

pub async fn get_task_by_id(pool: &SqlitePool, id: &str) -> Result<Option<TaskRow>> {
    let row = sqlx::query(
        "SELECT id, text, bucket, list_id, effort, notes, done, done_at, created_at, \
         source_transcript_id, parent_task_id FROM tasks WHERE id = ?",
    )
    .bind(id)
    .fetch_optional(pool)
    .await
    .map_err(|e| KonError::StorageError(format!("Get task failed: {e}")))?;

    Ok(row.map(task_row_from))
}

/// Patch-style update. Each `Option` that is `Some` overwrites the column;
/// `None` preserves the existing value via COALESCE. Returns the refreshed
/// row, or an error if `id` does not exist after the UPDATE.
///
/// Intentionally does not touch `done` / `done_at` — those go through the
/// dedicated `complete_task` / `uncomplete_task` commands because they also
/// set the server-side timestamp.
pub async fn update_task(
    pool: &SqlitePool,
    id: &str,
    text: Option<&str>,
    bucket: Option<&str>,
    list_id: Option<&str>,
    effort: Option<&str>,
    notes: Option<&str>,
) -> Result<TaskRow> {
    sqlx::query(
        "UPDATE tasks SET \
         text = COALESCE(?, text), \
         bucket = COALESCE(?, bucket), \
         list_id = COALESCE(?, list_id), \
         effort = COALESCE(?, effort), \
         notes = COALESCE(?, notes) \
         WHERE id = ?",
    )
    .bind(text)
    .bind(bucket)
    .bind(list_id)
    .bind(effort)
    .bind(notes)
    .bind(id)
    .execute(pool)
    .await
    .map_err(|e| KonError::StorageError(format!("Update task failed: {e}")))?;

    get_task_by_id(pool, id).await?.ok_or_else(|| {
        KonError::StorageError(format!("update_task: task {id} not found after update"))
    })
}

pub async fn insert_subtask(
    pool: &SqlitePool,
    id: &str,
    text: &str,
    parent_task_id: &str,
) -> Result<()> {
    sqlx::query("INSERT INTO tasks (id, text, bucket, parent_task_id) VALUES (?, ?, 'inbox', ?)")
        .bind(id)
        .bind(text)
        .bind(parent_task_id)
        .execute(pool)
        .await
        .map_err(|e| KonError::StorageError(format!("Insert subtask failed: {e}")))?;
    Ok(())
}

pub async fn list_subtasks(pool: &SqlitePool, parent_id: &str) -> Result<Vec<TaskRow>> {
    let rows = sqlx::query(
        "SELECT id, text, bucket, list_id, effort, notes, done, done_at, created_at, \
         source_transcript_id, parent_task_id \
         FROM tasks WHERE parent_task_id = ? ORDER BY created_at ASC",
    )
    .bind(parent_id)
    .fetch_all(pool)
    .await
    .map_err(|e| KonError::StorageError(format!("List subtasks failed: {e}")))?;

    Ok(rows.into_iter().map(task_row_from).collect())
}

/// Mark a subtask done. If all siblings are now done, auto-complete the parent.
/// Runs in a transaction so concurrent completions see consistent sibling counts.
pub async fn complete_subtask_and_check_parent(pool: &SqlitePool, subtask_id: &str) -> Result<()> {
    let mut tx = pool
        .begin()
        .await
        .map_err(|e| KonError::StorageError(format!("Begin transaction failed: {e}")))?;

    sqlx::query("UPDATE tasks SET done = 1, done_at = datetime('now') WHERE id = ?")
        .bind(subtask_id)
        .execute(&mut *tx)
        .await
        .map_err(|e| KonError::StorageError(format!("Complete subtask failed: {e}")))?;

    let parent_id: Option<String> =
        sqlx::query_scalar("SELECT parent_task_id FROM tasks WHERE id = ?")
            .bind(subtask_id)
            .fetch_one(&mut *tx)
            .await
            .map_err(|e| KonError::StorageError(format!("Get parent_task_id failed: {e}")))?;

    if let Some(pid) = parent_id {
        let pending: i64 =
            sqlx::query_scalar("SELECT COUNT(*) FROM tasks WHERE parent_task_id = ? AND done = 0")
                .bind(&pid)
                .fetch_one(&mut *tx)
                .await
                .map_err(|e| {
                    KonError::StorageError(format!("Count pending subtasks failed: {e}"))
                })?;

        if pending == 0 {
            sqlx::query("UPDATE tasks SET done = 1, done_at = datetime('now') WHERE id = ?")
                .bind(&pid)
                .execute(&mut *tx)
                .await
                .map_err(|e| KonError::StorageError(format!("Auto-complete parent failed: {e}")))?;
        }
    }

    tx.commit()
        .await
        .map_err(|e| KonError::StorageError(format!("Commit transaction failed: {e}")))?;

    Ok(())
}

pub async fn complete_task(pool: &SqlitePool, id: &str) -> Result<()> {
    sqlx::query("UPDATE tasks SET done = 1, done_at = datetime('now') WHERE id = ?")
        .bind(id)
        .execute(pool)
        .await
        .map_err(|e| KonError::StorageError(format!("Complete task failed: {e}")))?;
    Ok(())
}

pub async fn uncomplete_task(pool: &SqlitePool, id: &str) -> Result<()> {
    sqlx::query("UPDATE tasks SET done = 0, done_at = NULL WHERE id = ?")
        .bind(id)
        .execute(pool)
        .await
        .map_err(|e| KonError::StorageError(format!("Uncomplete task failed: {e}")))?;
    Ok(())
}

pub async fn delete_task(pool: &SqlitePool, id: &str) -> Result<()> {
    sqlx::query("DELETE FROM tasks WHERE id = ?")
        .bind(id)
        .execute(pool)
        .await
        .map_err(|e| KonError::StorageError(format!("Delete task failed: {e}")))?;
    Ok(())
}

// --- Settings CRUD ---

pub async fn set_setting(pool: &SqlitePool, key: &str, value: &str) -> Result<()> {
    sqlx::query("INSERT OR REPLACE INTO settings (key, value) VALUES (?, ?)")
        .bind(key)
        .bind(value)
        .execute(pool)
        .await
        .map_err(|e| KonError::StorageError(format!("Set setting failed: {e}")))?;
    Ok(())
}

pub async fn get_setting(pool: &SqlitePool, key: &str) -> Result<Option<String>> {
    let row = sqlx::query("SELECT value FROM settings WHERE key = ?")
        .bind(key)
        .fetch_optional(pool)
        .await
        .map_err(|e| KonError::StorageError(format!("Get setting failed: {e}")))?;
    Ok(row.map(|r| r.get("value")))
}

// --- Row types ---

#[derive(Debug, Clone)]
pub struct ProfileRow {
    pub id: String,
    pub name: String,
    pub initial_prompt: String,
    pub created_at: String,
}

#[derive(Debug, Clone)]
pub struct ProfileTermRow {
    pub id: String,
    pub profile_id: String,
    pub term: String,
    pub note: String,
    pub created_at: String,
}

#[derive(Debug, Clone)]
pub struct TranscriptRow {
    pub id: String,
    pub text: String,
    pub source: String,
    pub profile_id: String,
    pub title: Option<String>,
    pub audio_path: Option<String>,
    pub duration: f64,
    pub engine: Option<String>,
    pub model_id: Option<String>,
    pub inference_ms: Option<i64>,
    pub sample_rate: Option<i64>,
    pub audio_channels: Option<i64>,
    pub format_mode: Option<String>,
    pub remove_fillers: bool,
    pub british_english: bool,
    pub anti_hallucination: bool,
    pub created_at: String,
    // Task 2.5 — transcripts_meta (migration v5). Persists the UI metadata
    // that previously lived in the removed localStorage `kon_history` cache.
    pub starred: bool,
    pub manual_tags: String,
    pub template: String,
    pub language: String,
    pub segments_json: String,
}

#[derive(Debug, Clone)]
pub struct TaskRow {
    pub id: String,
    pub text: String,
    pub bucket: String,
    pub list_id: Option<String>,
    pub effort: Option<String>,
    pub notes: String,
    pub done: bool,
    pub done_at: Option<String>,
    pub created_at: String,
    pub source_transcript_id: Option<String>,
    pub parent_task_id: Option<String>,
}

fn transcript_row_from(r: &sqlx::sqlite::SqliteRow) -> TranscriptRow {
    TranscriptRow {
        id: r.get("id"),
        text: r.get("text"),
        source: r.get("source"),
        profile_id: r.get("profile_id"),
        title: r.get("title"),
        audio_path: r.get("audio_path"),
        duration: r.get("duration"),
        engine: r.get("engine"),
        model_id: r.get("model_id"),
        inference_ms: r.get("inference_ms"),
        sample_rate: r.get("sample_rate"),
        audio_channels: r.get("audio_channels"),
        format_mode: r.get("format_mode"),
        remove_fillers: r.get("remove_fillers"),
        british_english: r.get("british_english"),
        anti_hallucination: r.get("anti_hallucination"),
        created_at: r.get("created_at"),
        // Task 2.5 (migration v5). INTEGER 0/1 → bool via `!= 0`.
        starred: r.get::<i64, _>("starred") != 0,
        manual_tags: r.get("manual_tags"),
        template: r.get("template"),
        language: r.get("language"),
        segments_json: r.get("segments_json"),
    }
}

fn profile_row_from(r: &sqlx::sqlite::SqliteRow) -> ProfileRow {
    ProfileRow {
        id: r.get("id"),
        name: r.get("name"),
        initial_prompt: r.get("initial_prompt"),
        created_at: r.get("created_at"),
    }
}

fn profile_term_row_from(r: &sqlx::sqlite::SqliteRow) -> ProfileTermRow {
    ProfileTermRow {
        id: r.get("id"),
        profile_id: r.get("profile_id"),
        term: r.get("term"),
        note: r.get("note"),
        created_at: r.get("created_at"),
    }
}

fn task_row_from(r: sqlx::sqlite::SqliteRow) -> TaskRow {
    TaskRow {
        id: r.get("id"),
        text: r.get("text"),
        bucket: r.get("bucket"),
        list_id: r.get("list_id"),
        effort: r.get("effort"),
        notes: r.get("notes"),
        done: r.get("done"),
        done_at: r.get("done_at"),
        created_at: r.get("created_at"),
        source_transcript_id: r.get("source_transcript_id"),
        parent_task_id: r.get("parent_task_id"),
    }
}

// --- Profile CRUD (Phase 2 Task 11) ---
//
// Profiles partition the per-user transcription dictionary so that a single
// database can hold multiple named contexts (work, personal, a client, a
// project). Each profile owns an `initial_prompt` that steers Whisper's
// decoder and a list of `profile_terms` that bias recognition and feed
// post-transcription fix-ups.
//
// The `Default` profile (id = DEFAULT_PROFILE_ID, seeded by migration v6)
// must always exist and must never be renamed. Two layers enforce this:
//   1. The DB triggers `trg_protect_default_profile_delete` /
//      `trg_protect_default_profile_rename` from migration v6.
//   2. Rust-layer fail-fast checks below that short-circuit BEFORE the
//      query hits sqlite, so UI callers get a friendly KonError::StorageError
//      instead of an opaque `SQLITE_CONSTRAINT_TRIGGER` wrapped in text.

pub async fn list_profiles(pool: &SqlitePool) -> Result<Vec<ProfileRow>> {
    let rows =
        sqlx::query("SELECT id, name, initial_prompt, created_at FROM profiles ORDER BY name ASC")
            .fetch_all(pool)
            .await
            .map_err(|e| KonError::StorageError(format!("List profiles failed: {e}")))?;
    Ok(rows.iter().map(profile_row_from).collect())
}

pub async fn get_profile(pool: &SqlitePool, id: &str) -> Result<Option<ProfileRow>> {
    let row = sqlx::query("SELECT id, name, initial_prompt, created_at FROM profiles WHERE id = ?")
        .bind(id)
        .fetch_optional(pool)
        .await
        .map_err(|e| KonError::StorageError(format!("Get profile failed: {e}")))?;
    Ok(row.as_ref().map(profile_row_from))
}

pub async fn create_profile(
    pool: &SqlitePool,
    name: &str,
    initial_prompt: &str,
) -> Result<ProfileRow> {
    let id = uuid::Uuid::new_v4().to_string();
    let row = sqlx::query(
        "INSERT INTO profiles (id, name, initial_prompt, created_at) \
         VALUES (?, ?, ?, datetime('now')) \
         RETURNING id, name, initial_prompt, created_at",
    )
    .bind(&id)
    .bind(name)
    .bind(initial_prompt)
    .fetch_one(pool)
    .await
    .map_err(|e| KonError::StorageError(format!("Create profile failed: {e}")))?;
    Ok(profile_row_from(&row))
}

/// Full replace of `name` and `initial_prompt` for a profile.
///
/// Fails fast in Rust — before hitting sqlite — for two Default-profile
/// rules that the migration v6 trigger also enforces but returns opaquely:
///   * the Default id must not be renamed;
///   * no other profile may be renamed to "Default" (would collide with
///     the UNIQUE(name) constraint anyway, but we surface a better error).
pub async fn update_profile(
    pool: &SqlitePool,
    id: &str,
    name: &str,
    initial_prompt: &str,
) -> Result<()> {
    if id == crate::DEFAULT_PROFILE_ID && name != "Default" {
        return Err(KonError::StorageError(
            "Default profile cannot be renamed".into(),
        ));
    }
    if id != crate::DEFAULT_PROFILE_ID && name == "Default" {
        return Err(KonError::StorageError(
            "Cannot rename another profile to 'Default'".into(),
        ));
    }
    sqlx::query("UPDATE profiles SET name = ?, initial_prompt = ? WHERE id = ?")
        .bind(name)
        .bind(initial_prompt)
        .bind(id)
        .execute(pool)
        .await
        .map_err(|e| KonError::StorageError(format!("Update profile failed: {e}")))?;
    Ok(())
}

/// Delete a profile. Rejects the Default profile at the Rust layer so the
/// UI sees a human-readable message instead of a trigger ABORT string.
/// ON DELETE CASCADE on `profile_terms.profile_id` cleans up children.
pub async fn delete_profile(pool: &SqlitePool, id: &str) -> Result<()> {
    if id == crate::DEFAULT_PROFILE_ID {
        return Err(KonError::StorageError(
            "Default profile cannot be deleted".into(),
        ));
    }
    sqlx::query("DELETE FROM profiles WHERE id = ?")
        .bind(id)
        .execute(pool)
        .await
        .map_err(|e| KonError::StorageError(format!("Delete profile failed: {e}")))?;
    Ok(())
}

pub async fn list_profile_terms(
    pool: &SqlitePool,
    profile_id: &str,
) -> Result<Vec<ProfileTermRow>> {
    let rows = sqlx::query(
        "SELECT id, profile_id, term, note, created_at FROM profile_terms \
         WHERE profile_id = ? ORDER BY term ASC",
    )
    .bind(profile_id)
    .fetch_all(pool)
    .await
    .map_err(|e| KonError::StorageError(format!("List profile terms failed: {e}")))?;
    Ok(rows.iter().map(profile_term_row_from).collect())
}

/// UPSERT on UNIQUE(profile_id, term). Uses ON CONFLICT … DO UPDATE so that
/// re-adding an existing term simply refreshes the note and returns the
/// existing row (not a fresh id).
pub async fn add_profile_term(
    pool: &SqlitePool,
    profile_id: &str,
    term: &str,
    note: &str,
) -> Result<ProfileTermRow> {
    let new_id = uuid::Uuid::new_v4().to_string();
    let row = sqlx::query(
        "INSERT INTO profile_terms (id, profile_id, term, note, created_at) \
         VALUES (?, ?, ?, ?, datetime('now')) \
         ON CONFLICT(profile_id, term) DO UPDATE SET note = excluded.note \
         RETURNING id, profile_id, term, note, created_at",
    )
    .bind(&new_id)
    .bind(profile_id)
    .bind(term)
    .bind(note)
    .fetch_one(pool)
    .await
    .map_err(|e| KonError::StorageError(format!("Add profile term failed: {e}")))?;
    Ok(profile_term_row_from(&row))
}

pub async fn delete_profile_term(pool: &SqlitePool, id: &str) -> Result<()> {
    sqlx::query("DELETE FROM profile_terms WHERE id = ?")
        .bind(id)
        .execute(pool)
        .await
        .map_err(|e| KonError::StorageError(format!("Delete profile term failed: {e}")))?;
    Ok(())
}

// --- Error Logging ---

/// Log a structured error to the `error_log` table.
///
/// Available for Tauri command handlers to persist errors for diagnostics.
/// Each entry records context (which subsystem), an optional error code,
/// the human-readable message, and optional JSON metadata.
///
/// # Example
/// ```ignore
/// log_error(&pool, "transcription", Some("WHISPER_INIT"), "Model load failed", None).await?;
/// ```
///
/// TODO: Wire this into Tauri command error paths so runtime failures are
/// persisted for user-facing error history and crash diagnostics.
pub async fn log_error(
    pool: &SqlitePool,
    context: &str,
    error_code: Option<&str>,
    message: &str,
    metadata: Option<&str>,
) -> Result<()> {
    sqlx::query(
        "INSERT INTO error_log (context, error_code, message, metadata) VALUES (?, ?, ?, ?)",
    )
    .bind(context)
    .bind(error_code)
    .bind(message)
    .bind(metadata)
    .execute(pool)
    .await
    .map_err(|e| KonError::StorageError(format!("Error log failed: {e}")))?;
    Ok(())
}

/// Read the most recent N rows from `error_log`, newest first. Used by the
/// diagnostic-report bundler in Settings → About.
#[derive(Debug, Clone)]
pub struct ErrorLogRow {
    pub id: i64,
    pub timestamp: String,
    pub context: String,
    pub error_code: Option<String>,
    pub message: String,
    pub metadata: Option<String>,
}

pub async fn list_recent_errors(pool: &SqlitePool, limit: i64) -> Result<Vec<ErrorLogRow>> {
    let rows = sqlx::query(
        "SELECT id, timestamp, context, error_code, message, metadata \
         FROM error_log ORDER BY id DESC LIMIT ?",
    )
    .bind(limit)
    .fetch_all(pool)
    .await
    .map_err(|e| KonError::StorageError(format!("Read error_log failed: {e}")))?;

    Ok(rows
        .into_iter()
        .map(|r| ErrorLogRow {
            id: r.get("id"),
            timestamp: r.get("timestamp"),
            context: r.get("context"),
            error_code: r.get("error_code"),
            message: r.get("message"),
            metadata: r.get("metadata"),
        })
        .collect())
}

#[cfg(test)]
mod tests {
    use super::*;

    async fn test_pool() -> SqlitePool {
        let pool = SqlitePoolOptions::new()
            .connect("sqlite::memory:")
            .await
            .unwrap();
        run_migrations(&pool).await.unwrap();
        pool
    }

    #[tokio::test]
    async fn transcript_crud_roundtrip() {
        let pool = test_pool().await;

        insert_transcript(
            &pool,
            &InsertTranscriptParams {
                id: "t1",
                text: "Hello world",
                source: "microphone",
                profile_id: crate::DEFAULT_PROFILE_ID,
                title: Some("Test"),
                audio_path: None,
                duration: 1.5,
                engine: Some("whisper"),
                model_id: Some("whisper-tiny-en"),
                inference_ms: Some(250),
                sample_rate: Some(48000),
                audio_channels: Some(1),
                format_mode: Some("Clean"),
                remove_fillers: true,
                british_english: true,
                anti_hallucination: false,
            },
        )
        .await
        .unwrap();

        let t = get_transcript(&pool, "t1").await.unwrap().unwrap();
        assert_eq!(t.text, "Hello world");
        assert_eq!(t.source, "microphone");
        assert_eq!(t.profile_id, crate::DEFAULT_PROFILE_ID);
        assert_eq!(t.duration, 1.5);
        assert_eq!(t.engine.as_deref(), Some("whisper"));
        assert_eq!(t.model_id.as_deref(), Some("whisper-tiny-en"));
        assert_eq!(t.inference_ms, Some(250));
        assert!(t.remove_fillers);
        assert!(t.british_english);

        let list = list_transcripts(&pool, 10).await.unwrap();
        assert_eq!(list.len(), 1);

        delete_transcript(&pool, "t1").await.unwrap();
        let deleted = get_transcript(&pool, "t1").await.unwrap();
        assert!(deleted.is_none());
    }

    #[tokio::test]
    async fn search_transcripts_uses_fts5_and_ranks_by_relevance() {
        // Locks in the FTS5 behaviour contract: MATCH-based substring-like
        // search (case-insensitive tokens) with rank-ordered results, so
        // anyone swapping `search_transcripts` out for embeddings later
        // knows what invariants they must preserve.
        let pool = test_pool().await;

        let rows = [
            ("t1", "The Parakeet speech model runs on sherpa-onnx."),
            (
                "t2",
                "Parakeet parakeet parakeet dominates English benchmarks.",
            ),
            ("t3", "Whisper large-v3 remains the multilingual champion."),
        ];

        for (id, text) in rows {
            insert_transcript(
                &pool,
                &InsertTranscriptParams {
                    id,
                    text,
                    source: "microphone",
                    profile_id: crate::DEFAULT_PROFILE_ID,
                    title: None,
                    audio_path: None,
                    duration: 1.0,
                    engine: Some("whisper"),
                    model_id: Some("whisper-tiny-en"),
                    inference_ms: None,
                    sample_rate: None,
                    audio_channels: None,
                    format_mode: None,
                    remove_fillers: false,
                    british_english: false,
                    anti_hallucination: false,
                },
            )
            .await
            .unwrap();
        }

        let hits = search_transcripts(&pool, "parakeet", 10).await.unwrap();
        let ids: Vec<&str> = hits.iter().map(|row| row.id.as_str()).collect();
        assert_eq!(
            ids,
            vec!["t2", "t1"],
            "t3 has no 'parakeet' token; t2 outranks t1 on repetition"
        );

        let no_match = search_transcripts(&pool, "moonshine", 10).await.unwrap();
        assert!(no_match.is_empty());
    }

    #[tokio::test]
    async fn task_crud_roundtrip() {
        let pool = test_pool().await;

        insert_task(&pool, "task1", "Buy groceries", "today", None, None, None)
            .await
            .unwrap();

        let tasks = list_tasks(&pool).await.unwrap();
        assert_eq!(tasks.len(), 1);
        assert_eq!(tasks[0].text, "Buy groceries");
        assert!(!tasks[0].done);

        complete_task(&pool, "task1").await.unwrap();
        let tasks = list_tasks(&pool).await.unwrap();
        assert!(tasks[0].done);

        delete_task(&pool, "task1").await.unwrap();
        let tasks = list_tasks(&pool).await.unwrap();
        assert!(tasks.is_empty());
    }

    #[tokio::test]
    async fn subtask_crud_roundtrip() {
        let pool = test_pool().await;
        insert_task(&pool, "p1", "Write report", "inbox", None, None, None)
            .await
            .unwrap();
        insert_subtask(&pool, "s1", "Open document", "p1")
            .await
            .unwrap();
        insert_subtask(&pool, "s2", "Write introduction", "p1")
            .await
            .unwrap();

        // list_tasks must exclude subtasks
        let top_level = list_tasks(&pool).await.unwrap();
        assert_eq!(top_level.len(), 1);
        assert_eq!(top_level[0].id, "p1");

        // list_subtasks returns both children
        let subs = list_subtasks(&pool, "p1").await.unwrap();
        assert_eq!(subs.len(), 2);

        // completing s1 should NOT auto-complete parent (s2 still pending)
        complete_subtask_and_check_parent(&pool, "s1")
            .await
            .unwrap();
        let parent = get_task_by_id(&pool, "p1").await.unwrap().unwrap();
        assert!(!parent.done, "parent should not be done yet");

        // completing s2 SHOULD auto-complete parent
        complete_subtask_and_check_parent(&pool, "s2")
            .await
            .unwrap();
        let parent = get_task_by_id(&pool, "p1").await.unwrap().unwrap();
        assert!(
            parent.done,
            "parent should auto-complete when all children done"
        );
    }

    #[tokio::test]
    async fn update_transcript_meta_happy_path() {
        // Task 2.5 — insert a transcript, update starred=true, read it back.
        let pool = test_pool().await;
        insert_transcript(
            &pool,
            &InsertTranscriptParams {
                id: "tm1",
                text: "body",
                source: "microphone",
                profile_id: crate::DEFAULT_PROFILE_ID,
                title: Some("Meta happy"),
                audio_path: None,
                duration: 0.0,
                engine: None,
                model_id: None,
                inference_ms: None,
                sample_rate: None,
                audio_channels: None,
                format_mode: None,
                remove_fillers: false,
                british_english: false,
                anti_hallucination: false,
            },
        )
        .await
        .unwrap();

        let row = update_transcript_meta(&pool, "tm1", Some(true), None, None, None, None)
            .await
            .unwrap();
        assert!(row.starred, "starred must flip true after update");

        let fetched = get_transcript(&pool, "tm1").await.unwrap().unwrap();
        assert!(fetched.starred, "starred must persist via SELECT");
        assert_eq!(fetched.manual_tags, "");
        assert_eq!(fetched.template, "");
        assert_eq!(fetched.language, "");
        assert_eq!(fetched.segments_json, "");
    }

    #[tokio::test]
    async fn update_transcript_meta_partial_leaves_others_unchanged() {
        // Task 2.5 — partial update: only segments_json changes; the other
        // meta columns must be preserved by COALESCE.
        let pool = test_pool().await;
        insert_transcript(
            &pool,
            &InsertTranscriptParams {
                id: "tm2",
                text: "body",
                source: "microphone",
                profile_id: crate::DEFAULT_PROFILE_ID,
                title: None,
                audio_path: None,
                duration: 0.0,
                engine: None,
                model_id: None,
                inference_ms: None,
                sample_rate: None,
                audio_channels: None,
                format_mode: None,
                remove_fillers: false,
                british_english: false,
                anti_hallucination: false,
            },
        )
        .await
        .unwrap();

        // Seed starred + tags + template + language first.
        update_transcript_meta(
            &pool,
            "tm2",
            Some(true),
            Some("urgent,review"),
            Some("Meeting"),
            Some("en-GB"),
            None,
        )
        .await
        .unwrap();

        // Now update only segments_json.
        let row = update_transcript_meta(
            &pool,
            "tm2",
            None,
            None,
            None,
            None,
            Some(r#"[{"start":0.0,"end":1.0,"text":"hi"}]"#),
        )
        .await
        .unwrap();

        assert!(row.starred, "starred must survive partial update");
        assert_eq!(row.manual_tags, "urgent,review");
        assert_eq!(row.template, "Meeting");
        assert_eq!(row.language, "en-GB");
        assert_eq!(
            row.segments_json,
            r#"[{"start":0.0,"end":1.0,"text":"hi"}]"#
        );
    }

    #[tokio::test]
    async fn update_task_overwrites_provided_fields() {
        // Task 2.6 — happy path: insert, update bucket + effort, read back.
        let pool = test_pool().await;
        insert_task(&pool, "u1", "Draft post", "inbox", None, None, None)
            .await
            .unwrap();

        let row = update_task(&pool, "u1", None, Some("today"), None, Some("15m"), None)
            .await
            .unwrap();
        assert_eq!(row.bucket, "today");
        assert_eq!(row.effort.as_deref(), Some("15m"));
        assert_eq!(row.text, "Draft post", "text must be unchanged");
        assert_eq!(row.notes, "", "notes default must survive");
    }

    #[tokio::test]
    async fn update_task_partial_leaves_others_unchanged() {
        // Task 2.6 — partial update: only notes changes; text/bucket intact.
        let pool = test_pool().await;
        insert_task(&pool, "u2", "Prep slides", "today", None, None, Some("30m"))
            .await
            .unwrap();

        let row = update_task(
            &pool,
            "u2",
            None,
            None,
            None,
            None,
            Some("remember venue wifi"),
        )
        .await
        .unwrap();
        assert_eq!(row.text, "Prep slides");
        assert_eq!(row.bucket, "today");
        assert_eq!(row.effort.as_deref(), Some("30m"));
        assert_eq!(row.notes, "remember venue wifi");
    }

    #[tokio::test]
    async fn update_task_missing_id_errors() {
        let pool = test_pool().await;
        let res = update_task(&pool, "missing", Some("x"), None, None, None, None).await;
        assert!(
            res.is_err(),
            "update_task must error when id does not exist"
        );
    }

    // --- Profile CRUD tests (Task 11) ---

    #[tokio::test]
    async fn list_profiles_returns_seeded_default() {
        // Happy path: a fresh DB has exactly the seeded Default profile.
        let pool = test_pool().await;
        let profiles = list_profiles(&pool).await.unwrap();
        assert_eq!(profiles.len(), 1, "Default profile must be seeded");
        assert_eq!(profiles[0].id, crate::DEFAULT_PROFILE_ID);
        assert_eq!(profiles[0].name, "Default");
    }

    #[tokio::test]
    async fn get_profile_happy_path_and_missing() {
        let pool = test_pool().await;
        let p = get_profile(&pool, crate::DEFAULT_PROFILE_ID).await.unwrap();
        assert!(p.is_some());
        assert_eq!(p.unwrap().name, "Default");

        // Guardrail: unknown id returns None, not an error.
        let missing = get_profile(&pool, "00000000-0000-0000-0000-000000000099")
            .await
            .unwrap();
        assert!(missing.is_none());
    }

    #[tokio::test]
    async fn create_profile_happy_path() {
        let pool = test_pool().await;
        let row = create_profile(&pool, "Work", "You are a meeting notes assistant.")
            .await
            .unwrap();
        assert_eq!(row.name, "Work");
        assert_eq!(row.initial_prompt, "You are a meeting notes assistant.");
        assert!(!row.id.is_empty());
        assert_ne!(row.id, crate::DEFAULT_PROFILE_ID);

        let profiles = list_profiles(&pool).await.unwrap();
        assert_eq!(profiles.len(), 2, "Default + Work");
    }

    #[tokio::test]
    async fn create_profile_rejects_duplicate_name() {
        // Guardrail: UNIQUE(name) collision surfaces as StorageError.
        let pool = test_pool().await;
        create_profile(&pool, "Personal", "").await.unwrap();
        let res = create_profile(&pool, "Personal", "").await;
        assert!(res.is_err(), "duplicate name must fail");
    }

    #[tokio::test]
    async fn update_profile_happy_path() {
        let pool = test_pool().await;
        let created = create_profile(&pool, "ClientA", "prompt v1").await.unwrap();
        update_profile(&pool, &created.id, "ClientA Renamed", "prompt v2")
            .await
            .unwrap();
        let fetched = get_profile(&pool, &created.id).await.unwrap().unwrap();
        assert_eq!(fetched.name, "ClientA Renamed");
        assert_eq!(fetched.initial_prompt, "prompt v2");
    }

    #[tokio::test]
    async fn update_profile_rejects_renaming_default() {
        // Guardrail: Rust layer rejects renaming the Default id; must not
        // reach sqlite trigger.
        let pool = test_pool().await;
        let res = update_profile(&pool, crate::DEFAULT_PROFILE_ID, "NotDefault", "").await;
        let msg = format!("{}", res.expect_err("must reject rename of Default"));
        assert!(
            msg.contains("Default profile cannot be renamed"),
            "got: {msg}"
        );
    }

    #[tokio::test]
    async fn update_profile_rejects_renaming_other_to_default() {
        // Guardrail: another profile cannot adopt the name "Default".
        let pool = test_pool().await;
        let other = create_profile(&pool, "Temp", "").await.unwrap();
        let res = update_profile(&pool, &other.id, "Default", "").await;
        let msg = format!("{}", res.expect_err("must reject adopting 'Default'"));
        assert!(msg.contains("Default"), "got: {msg}");
    }

    #[tokio::test]
    async fn delete_profile_happy_path() {
        let pool = test_pool().await;
        let created = create_profile(&pool, "Ephemeral", "").await.unwrap();
        delete_profile(&pool, &created.id).await.unwrap();
        let fetched = get_profile(&pool, &created.id).await.unwrap();
        assert!(fetched.is_none());
    }

    #[tokio::test]
    async fn delete_profile_rejects_default() {
        let pool = test_pool().await;
        let res = delete_profile(&pool, crate::DEFAULT_PROFILE_ID).await;
        let msg = format!("{}", res.expect_err("must reject delete of Default"));
        assert!(
            msg.contains("Default profile cannot be deleted"),
            "got: {msg}"
        );
        // And Default must still be listed.
        let profiles = list_profiles(&pool).await.unwrap();
        assert_eq!(profiles.len(), 1);
    }

    #[tokio::test]
    async fn list_profile_terms_happy_path() {
        let pool = test_pool().await;
        let p = create_profile(&pool, "WithTerms", "").await.unwrap();
        add_profile_term(&pool, &p.id, "CORBEL", "company name")
            .await
            .unwrap();
        add_profile_term(&pool, &p.id, "Wren", "agent name")
            .await
            .unwrap();
        let terms = list_profile_terms(&pool, &p.id).await.unwrap();
        assert_eq!(terms.len(), 2);
        // Sorted alphabetically.
        assert_eq!(terms[0].term, "CORBEL");
        assert_eq!(terms[1].term, "Wren");
    }

    #[tokio::test]
    async fn list_profile_terms_filters_by_profile() {
        // Guardrail: terms from one profile must not leak into another.
        let pool = test_pool().await;
        let a = create_profile(&pool, "A", "").await.unwrap();
        let b = create_profile(&pool, "B", "").await.unwrap();
        add_profile_term(&pool, &a.id, "alpha", "").await.unwrap();
        add_profile_term(&pool, &b.id, "beta", "").await.unwrap();
        let a_terms = list_profile_terms(&pool, &a.id).await.unwrap();
        let b_terms = list_profile_terms(&pool, &b.id).await.unwrap();
        assert_eq!(a_terms.len(), 1);
        assert_eq!(a_terms[0].term, "alpha");
        assert_eq!(b_terms.len(), 1);
        assert_eq!(b_terms[0].term, "beta");
    }

    #[tokio::test]
    async fn add_profile_term_happy_path() {
        let pool = test_pool().await;
        let p = create_profile(&pool, "P", "").await.unwrap();
        let row = add_profile_term(&pool, &p.id, "ACME", "client")
            .await
            .unwrap();
        assert_eq!(row.term, "ACME");
        assert_eq!(row.note, "client");
        assert_eq!(row.profile_id, p.id);
    }

    #[tokio::test]
    async fn add_profile_term_upsert_on_duplicate_term() {
        // Guardrail: UPSERT refreshes note and reuses the existing row id
        // rather than violating UNIQUE(profile_id, term).
        let pool = test_pool().await;
        let p = create_profile(&pool, "P2", "").await.unwrap();
        let first = add_profile_term(&pool, &p.id, "ACME", "first note")
            .await
            .unwrap();
        let second = add_profile_term(&pool, &p.id, "ACME", "second note")
            .await
            .unwrap();
        assert_eq!(first.id, second.id, "UPSERT must preserve row id");
        assert_eq!(second.note, "second note");
        let terms = list_profile_terms(&pool, &p.id).await.unwrap();
        assert_eq!(terms.len(), 1, "UPSERT must not create a second row");
    }

    #[tokio::test]
    async fn delete_profile_term_happy_path() {
        let pool = test_pool().await;
        let p = create_profile(&pool, "P3", "").await.unwrap();
        let t = add_profile_term(&pool, &p.id, "Foo", "").await.unwrap();
        delete_profile_term(&pool, &t.id).await.unwrap();
        let remaining = list_profile_terms(&pool, &p.id).await.unwrap();
        assert!(remaining.is_empty());
    }

    #[tokio::test]
    async fn delete_profile_term_missing_id_is_noop() {
        // Guardrail: deleting a non-existent term must not error; sqlite
        // DELETE on zero rows is a success. Verifies we're not hand-rolling
        // an existence check that would regress the UI's happy path on
        // double-click delete.
        let pool = test_pool().await;
        let p = create_profile(&pool, "NoopProfile", "").await.unwrap();
        let t = add_profile_term(&pool, &p.id, "real", "").await.unwrap();
        delete_profile_term(&pool, "00000000-dead-beef-0000-000000000000")
            .await
            .expect("missing id must be a silent no-op");
        // Real term still exists.
        let terms = list_profile_terms(&pool, &p.id).await.unwrap();
        assert_eq!(terms.len(), 1);
        assert_eq!(terms[0].id, t.id);
    }

    #[tokio::test]
    async fn delete_profile_cascades_to_terms() {
        // Guardrail: deleting a profile must cascade-delete its terms via
        // ON DELETE CASCADE (migration v6 FK).
        let pool = test_pool().await;
        let p = create_profile(&pool, "Doomed", "").await.unwrap();
        add_profile_term(&pool, &p.id, "x", "").await.unwrap();
        add_profile_term(&pool, &p.id, "y", "").await.unwrap();
        delete_profile(&pool, &p.id).await.unwrap();
        // Using a raw query because list_profile_terms would happily
        // return [] even if the FK had been forgotten. We want to prove
        // the child rows are actually gone from the table.
        let count: i64 =
            sqlx::query_scalar("SELECT COUNT(*) FROM profile_terms WHERE profile_id = ?")
                .bind(&p.id)
                .fetch_one(&pool)
                .await
                .unwrap();
        assert_eq!(count, 0, "terms must cascade-delete with their profile");
    }

    #[tokio::test]
    async fn settings_crud_roundtrip() {
        let pool = test_pool().await;

        set_setting(&pool, "theme", "dark").await.unwrap();
        let val = get_setting(&pool, "theme").await.unwrap();
        assert_eq!(val.as_deref(), Some("dark"));

        set_setting(&pool, "theme", "light").await.unwrap();
        let val = get_setting(&pool, "theme").await.unwrap();
        assert_eq!(val.as_deref(), Some("light"));

        let missing = get_setting(&pool, "nonexistent").await.unwrap();
        assert!(missing.is_none());
    }
}
