/// Internal Delete builder for {{ model_name }} — use `{{ delegate_name }}::delete(filter)` instead.
pub(crate) struct {{ delete_name }}<E: crate::Executor> {
    client: crate::Client<E>,
    filter: Option<nautilus_core::Expr>,
    require_filter: bool,
}

impl<E> {{ delete_name }}<E>
where
    E: crate::Executor,
    for<'a> E::Row<'a>: Into<crate::Row>,
{
    pub(crate) fn new(client: crate::Client<E>) -> Self {
        {{ delete_name }} {
            client,
            filter: None,
            require_filter: true,
        }
    }

    /// Set WHERE filter.
    #[must_use]
    pub(crate) fn where_(mut self, expr: nautilus_core::Expr) -> Self {
        self.filter = Some(expr);
        self
    }

    /// Delete all rows (removes the safety guard that requires a filter).
    #[must_use]
    pub(crate) fn all(mut self) -> Self {
        self.require_filter = false;
        self
    }

    /// Execute the delete, returning deleted rows via `RETURNING`.
    ///
    /// Returns an empty `Vec` for databases that do not support `RETURNING`
    /// (e.g. MySQL).
    pub(crate) async fn exec(self) -> nautilus_core::Result<Vec<{{ model_name }}>> {
        use nautilus_core::*;
        use std::convert::Into;

        if self.require_filter && self.filter.is_none() {
            return Err(Error::InvalidQuery(
                "Delete requires a WHERE filter — call `.all()` to delete every row.".to_string(),
            ));
        }

        let mut builder = Delete::from_table("{{ table_name }}").with_capacity(DeleteCapacity {
            returning: usize::from(self.client.dialect().supports_returning()) * {{ all_scalar_fields | length }},
        });
        if let Some(filter) = self.filter {
            builder = builder.filter(filter);
        }

        let supports_returning = self.client.dialect().supports_returning();
        if supports_returning {
            let returning_cols = vec![
{%- for field in all_scalar_fields %}
                ColumnMarker::from_static("{{ table_name }}", "{{ field.db_name }}"),
{%- endfor %}
            ];
            builder = builder.returning(returning_cols);
        }

        let delete = builder.build()?;
        let sql = self.client.dialect().render_delete_owned(delete)?;

        let rows = crate::execute_all(self.client.executor(), &sql)
            .await
            .map_err(|e| Error::Other(e.to_string()))?;

        let mut results = Vec::with_capacity(rows.len());
        for row in rows {
            let row: crate::Row = row.into();
            results.push(decode_model_row_with_hints(
                row,
                all_scalar_decode_slots(),
                all_scalar_value_hints(),
            )?);
        }

        Ok(results)
    }
}
