//! CRUD over the `routes` table.
//!
//! The orchestrator's `AppRouteTables` is repopulated from this repo
//! after every write — see the route_admin module for the binding.

use async_trait::async_trait;
use picloud_shared::{AppId, DispatchMode, HostKind, PathKind, Route, ScriptId};
use sqlx::PgPool;
use uuid::Uuid;

use crate::repo::ScriptRepositoryError;

#[derive(Debug, Clone)]
pub struct NewRoute {
    pub app_id: AppId,
    pub script_id: ScriptId,
    pub host_kind: HostKind,
    pub host: String,
    pub host_param_name: Option<String>,
    pub path_kind: PathKind,
    pub path: String,
    pub method: Option<String>,
    pub dispatch_mode: DispatchMode,
    /// Three-state lifecycle (§4.3). Create active by default.
    pub enabled: bool,
}

#[async_trait]
pub trait RouteRepository: Send + Sync {
    async fn list_all(&self) -> Result<Vec<Route>, ScriptRepositoryError>;
    /// Single-row lookup. Used by `DELETE /api/v1/admin/routes/{id}` so
    /// the capability check binds to the route's actual `app_id`
    /// (not a path param).
    async fn get(&self, route_id: Uuid) -> Result<Option<Route>, ScriptRepositoryError>;
    async fn list_for_app(&self, app_id: AppId) -> Result<Vec<Route>, ScriptRepositoryError>;
    async fn list_for_script(
        &self,
        script_id: ScriptId,
    ) -> Result<Vec<Route>, ScriptRepositoryError>;
    async fn create(&self, input: NewRoute) -> Result<Route, ScriptRepositoryError>;
    async fn delete(&self, route_id: Uuid) -> Result<(), ScriptRepositoryError>;
    /// Count routes whose host_kind/host pair matches a pattern in
    /// `app_id`. Used by the domain-claim delete guard.
    async fn count_for_app_host(
        &self,
        app_id: AppId,
        host_kind: HostKind,
        host: &str,
    ) -> Result<i64, ScriptRepositoryError>;
}

pub struct PostgresRouteRepository {
    pool: PgPool,
}

impl PostgresRouteRepository {
    #[must_use]
    pub fn new(pool: PgPool) -> Self {
        Self { pool }
    }
}

#[async_trait]
impl RouteRepository for PostgresRouteRepository {
    async fn list_all(&self) -> Result<Vec<Route>, ScriptRepositoryError> {
        let rows = sqlx::query_as::<_, RouteRow>(
            "SELECT id, app_id, script_id, host_kind, host, host_param_name, \
                    path_kind, path, method, dispatch_mode, enabled, created_at \
             FROM routes ORDER BY created_at",
        )
        .fetch_all(&self.pool)
        .await?;
        Ok(rows.into_iter().map(Into::into).collect())
    }

    async fn get(&self, route_id: Uuid) -> Result<Option<Route>, ScriptRepositoryError> {
        let row = sqlx::query_as::<_, RouteRow>(
            "SELECT id, app_id, script_id, host_kind, host, host_param_name, \
                    path_kind, path, method, dispatch_mode, enabled, created_at \
             FROM routes WHERE id = $1",
        )
        .bind(route_id)
        .fetch_optional(&self.pool)
        .await?;
        Ok(row.map(Into::into))
    }

    async fn list_for_app(&self, app_id: AppId) -> Result<Vec<Route>, ScriptRepositoryError> {
        let rows = sqlx::query_as::<_, RouteRow>(
            "SELECT id, app_id, script_id, host_kind, host, host_param_name, \
                    path_kind, path, method, dispatch_mode, enabled, created_at \
             FROM routes WHERE app_id = $1 ORDER BY created_at",
        )
        .bind(app_id.into_inner())
        .fetch_all(&self.pool)
        .await?;
        Ok(rows.into_iter().map(Into::into).collect())
    }

    async fn list_for_script(
        &self,
        script_id: ScriptId,
    ) -> Result<Vec<Route>, ScriptRepositoryError> {
        let rows = sqlx::query_as::<_, RouteRow>(
            "SELECT id, app_id, script_id, host_kind, host, host_param_name, \
                    path_kind, path, method, dispatch_mode, enabled, created_at \
             FROM routes WHERE script_id = $1 ORDER BY created_at",
        )
        .bind(script_id.into_inner())
        .fetch_all(&self.pool)
        .await?;
        Ok(rows.into_iter().map(Into::into).collect())
    }

    async fn create(&self, input: NewRoute) -> Result<Route, ScriptRepositoryError> {
        let mut tx = self.pool.begin().await?;
        let route = insert_route_tx(&mut tx, &input).await?;
        tx.commit().await?;
        Ok(route)
    }

    async fn delete(&self, route_id: Uuid) -> Result<(), ScriptRepositoryError> {
        let res = sqlx::query("DELETE FROM routes WHERE id = $1")
            .bind(route_id)
            .execute(&self.pool)
            .await?;
        if res.rows_affected() == 0 {
            return Err(ScriptRepositoryError::NotFound(ScriptId::from(route_id)));
        }
        Ok(())
    }

    async fn count_for_app_host(
        &self,
        app_id: AppId,
        host_kind: HostKind,
        host: &str,
    ) -> Result<i64, ScriptRepositoryError> {
        let count: (i64,) = sqlx::query_as(
            "SELECT COUNT(*) FROM routes \
             WHERE app_id = $1 AND host_kind = $2 AND host = $3",
        )
        .bind(app_id.into_inner())
        .bind(host_kind_str(host_kind))
        .bind(host)
        .fetch_one(&self.pool)
        .await?;
        Ok(count.0)
    }
}

const fn host_kind_str(k: HostKind) -> &'static str {
    match k {
        HostKind::Any => "any",
        HostKind::Strict => "strict",
        HostKind::Wildcard => "wildcard",
    }
}

const fn path_kind_str(k: PathKind) -> &'static str {
    match k {
        PathKind::Exact => "exact",
        PathKind::Prefix => "prefix",
        PathKind::Param => "param",
    }
}

/// Insert a route within an existing transaction (declarative apply
/// composes scripts + routes + triggers into one tx). Mirrors `create`
/// minus the `begin`/`commit`.
pub(crate) async fn insert_route_tx(
    tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
    input: &NewRoute,
) -> Result<Route, ScriptRepositoryError> {
    let res = sqlx::query_as::<_, RouteRow>(
        "INSERT INTO routes ( \
            app_id, script_id, host_kind, host, host_param_name, \
            path_kind, path, method, dispatch_mode, enabled \
         ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10) \
         RETURNING id, app_id, script_id, host_kind, host, host_param_name, \
                   path_kind, path, method, dispatch_mode, enabled, created_at",
    )
    .bind(input.app_id.into_inner())
    .bind(input.script_id.into_inner())
    .bind(host_kind_str(input.host_kind))
    .bind(&input.host)
    .bind(input.host_param_name.as_deref())
    .bind(path_kind_str(input.path_kind))
    .bind(&input.path)
    .bind(input.method.as_deref())
    .bind(input.dispatch_mode.as_str())
    .bind(input.enabled)
    .fetch_one(&mut **tx)
    .await;
    match res {
        Ok(row) => Ok(row.into()),
        Err(sqlx::Error::Database(e)) if e.is_unique_violation() => Err(
            ScriptRepositoryError::Conflict("a route with this binding already exists".into()),
        ),
        Err(sqlx::Error::Database(e)) if e.is_foreign_key_violation() => {
            Err(ScriptRepositoryError::NotFound(input.script_id))
        }
        Err(e) => Err(e.into()),
    }
}

/// Delete a route by id within an existing transaction.
///
/// Unlike the non-tx [`RouteRepository::delete`], this is intentionally
/// idempotent: a missing row is not an error. The only caller is the
/// reconcile engine (`ApplyService`), where "delete a route already gone"
/// (e.g. removed out-of-band between the diff read and the write) is a
/// no-op to converge on, not a failure to roll back the whole apply.
pub(crate) async fn delete_route_tx(
    tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
    route_id: Uuid,
) -> Result<(), ScriptRepositoryError> {
    sqlx::query("DELETE FROM routes WHERE id = $1")
        .bind(route_id)
        .execute(&mut **tx)
        .await?;
    Ok(())
}

#[derive(sqlx::FromRow)]
struct RouteRow {
    id: Uuid,
    app_id: Uuid,
    script_id: Uuid,
    host_kind: String,
    host: String,
    host_param_name: Option<String>,
    path_kind: String,
    path: String,
    method: Option<String>,
    dispatch_mode: String,
    enabled: bool,
    created_at: chrono::DateTime<chrono::Utc>,
}

impl From<RouteRow> for Route {
    fn from(r: RouteRow) -> Self {
        Self {
            id: r.id,
            app_id: r.app_id.into(),
            script_id: r.script_id.into(),
            host_kind: match r.host_kind.as_str() {
                "strict" => HostKind::Strict,
                "wildcard" => HostKind::Wildcard,
                _ => HostKind::Any,
            },
            host: r.host,
            host_param_name: r.host_param_name,
            path_kind: match r.path_kind.as_str() {
                "prefix" => PathKind::Prefix,
                "param" => PathKind::Param,
                _ => PathKind::Exact,
            },
            path: r.path,
            method: r.method,
            dispatch_mode: DispatchMode::from_wire(&r.dispatch_mode).unwrap_or(DispatchMode::Sync),
            enabled: r.enabled,
            created_at: r.created_at,
        }
    }
}
