use std::fmt;
use std::fmt::Formatter;
use serde::{Deserialize, Serialize};

#[repr(i64)]
#[derive(Deserialize, Serialize, Copy, Clone, PartialEq, sqlx::Type)]
pub enum Origin {
    EU,
    NonEU,
    EUnonEU,
}
impl Origin {
    pub const VEC: &'static [Origin] = &[Origin::EU, Origin::NonEU, Origin::EUnonEU];

    pub fn position(&self) -> usize {
        Origin::VEC.iter().position(|r| *r == *self).unwrap()
    }
}

fn to_string(unit: Origin, f: &mut Formatter) -> fmt::Result {
    match unit {
        Origin::EU => write!(f, "EU"),
        Origin::NonEU => write!(f, "Nicht EU"),
        Origin::EUnonEU => write!(f, "EU / Nicht EU"),
    }
}

impl fmt::Debug for Origin {
    fn fmt(&self, f: &mut Formatter) -> fmt::Result {
        to_string(*self, f)
    }
}

impl fmt::Display for Origin {
    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
        to_string(*self, f)
    }
}
