use crate::types::validation_errors::ValidationError;
use chrono::NaiveDate;
use serde::{Deserialize, Serialize};
use std::fmt;

/// Data ISO 8601 apenas com data (AAAA-MM-DD)
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
#[repr(transparent)]
pub struct DateIso8601(String);

impl DateIso8601 {
    /// Cria uma nova data validando o formato AAAA-MM-DD
    pub fn new(value: impl Into<String>) -> Result<Self, ValidationError> {
        let value = value.into();

        if NaiveDate::parse_from_str(&value, "%Y-%m-%d").is_err() {
            return Err(ValidationError::InvalidFormat {
                field: "date".to_string(),
                message: format!("invalid date, expect YYYY-MM-DD, received: {}", value),
            });
        }

        Ok(Self(value))
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }
}

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