use std::fmt::Display;

use derive_more::derive::From;

use super::query::{dml::DML, SQLQuery, TableReference};

#[derive(From, Debug, Clone, PartialEq, Eq)]
pub enum Statement {
    SQLQuery(SQLQuery),
    DML(DML),
}

impl Statement {
    pub fn fmt_indented(
        &self,
        f: &mut std::fmt::Formatter<'_>,
        indentation: usize,
    ) -> std::fmt::Result {
        match self {
            Statement::SQLQuery(query) => query.fmt_indented(f, indentation),
            Statement::DML(dml) => dml.fmt_indented(f, indentation),
        }
    }

    pub fn get_table_references(&self) -> Vec<TableReference> {
        match self {
            Statement::SQLQuery(query) => query.get_table_references(),
            Statement::DML(DML::Insert(ins)) => ins.query.get_table_references(),
            Statement::DML(DML::Merge(mrg)) => mrg.query.get_table_references(),
        }
    }
}

impl Default for Statement {
    fn default() -> Self {
        Statement::SQLQuery(SQLQuery::default())
    }
}

impl Display for Statement {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        self.fmt_indented(f, 0)
    }
}
