/*
 * clickhouse-native-rs — Asynchronous ClickHouse client for Rust over the native TCP protocol
 * Copyright (c) 2026 Alexander Panasenko
 *
 * Contact: alex@prod.codes
 * Author: https://prod.codes/about/
 * Project: https://github.com/alex09x/clickhouse-native-rs
 * SPDX-License-Identifier: MIT
 */

//! `IntervalSecond`, `IntervalDay` and the other interval types: an `Int64`
//! count of a unit, the unit being part of the type (`DataTypeInterval`).

use std::fmt;

/// The unit of an interval type, `IntervalKind` in ClickHouse.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum IntervalKind {
    Nanosecond,
    Microsecond,
    Millisecond,
    Second,
    Minute,
    Hour,
    Day,
    Week,
    Month,
    Quarter,
    Year,
}

const KINDS: [IntervalKind; 11] = [
    IntervalKind::Nanosecond,
    IntervalKind::Microsecond,
    IntervalKind::Millisecond,
    IntervalKind::Second,
    IntervalKind::Minute,
    IntervalKind::Hour,
    IntervalKind::Day,
    IntervalKind::Week,
    IntervalKind::Month,
    IntervalKind::Quarter,
    IntervalKind::Year,
];

impl IntervalKind {
    /// The unit as the type name has it: `Second` in `IntervalSecond`.
    pub fn name(self) -> &'static str {
        match self {
            IntervalKind::Nanosecond => "Nanosecond",
            IntervalKind::Microsecond => "Microsecond",
            IntervalKind::Millisecond => "Millisecond",
            IntervalKind::Second => "Second",
            IntervalKind::Minute => "Minute",
            IntervalKind::Hour => "Hour",
            IntervalKind::Day => "Day",
            IntervalKind::Week => "Week",
            IntervalKind::Month => "Month",
            IntervalKind::Quarter => "Quarter",
            IntervalKind::Year => "Year",
        }
    }

    /// The unit of an interval type name such as `IntervalSecond`.
    pub(crate) fn from_type_name(type_name: &str) -> Option<Self> {
        let name = type_name.strip_prefix("Interval")?;
        KINDS.into_iter().find(|kind| kind.name() == name)
    }
}

impl fmt::Display for IntervalKind {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str(self.name())
    }
}

/// A value of an interval type: `count` of `kind`, as `INTERVAL 5 SECOND`.
///
/// ```rust
/// # use clickhouse_native_rs::types::{Interval, IntervalKind};
/// let interval = Interval::new(-2, IntervalKind::Month);
/// assert_eq!((interval.count, interval.kind), (-2, IntervalKind::Month));
/// ```
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
pub struct Interval {
    pub count: i64,
    pub kind: IntervalKind,
}

impl Interval {
    pub fn new(count: i64, kind: IntervalKind) -> Self {
        Interval { count, kind }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_type_names() {
        for kind in KINDS {
            let type_name = format!("Interval{kind}");
            assert_eq!(IntervalKind::from_type_name(&type_name), Some(kind));
        }
        assert_eq!(IntervalKind::from_type_name("IntervalFortnight"), None);
        assert_eq!(IntervalKind::from_type_name("Second"), None);
    }
}