use super::SQLExpression;
use std::fmt;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ExtractField {
    Year,
    Month,
    Day,
    DayOfWeek,
    Hour,
    Minute,
    Second,
}

impl fmt::Display for ExtractField {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ExtractField::Day => write!(f, "DAY"),
            ExtractField::DayOfWeek => write!(f, "DOW"),
            ExtractField::Month => write!(f, "MONTH"),
            ExtractField::Year => write!(f, "YEAR"),
            ExtractField::Hour => write!(f, "HOUR"),
            ExtractField::Minute => write!(f, "MINUTE"),
            ExtractField::Second => write!(f, "SECOND"),
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExtractFunction {
    pub field: ExtractField,
    pub value: Box<SQLExpression>,
}

impl ExtractFunction {
    pub fn new(field: ExtractField, value: SQLExpression) -> Self {
        Self {
            field,
            value: Box::new(value),
        }
    }

    pub fn fmt_indented(&self, f: &mut fmt::Formatter<'_>, _indentation: usize) -> fmt::Result {
        write!(f, "extract({} FROM {})", self.field, self.value)
    }
}

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