use std::{fmt::Debug, str::FromStr};

use thiserror::Error;

#[derive(Debug, Clone, Error)]
#[error("Invalid operator `{0}`")]
pub struct InvalidOperatorError(String);

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Operator {
    Lt,
    Lte,
    Eq,
    Gt,
    Gte,
    Like,
    ILike,
    STEVecOperator(STEVecOperator),

    #[deprecated(note = "Use Result and InvalidOperatorError instead", since = "0.2.4")]
    Unsupported,
}

/// These are all binary operators (*probably* PG specific).
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum STEVecOperator {
    LeftContainsRight, // @>
    RightContainsLeft, // <@
    Selector,          // ->
}

impl Operator {
    pub fn as_str(&self) -> &'static str {
        match self {
            Self::Lt => "<",
            Self::Lte => "<=",
            Self::Eq => "=",
            Self::Gt => ">",
            Self::Gte => ">=",
            Self::Like => "~~",   //Note: This is PostgreSQL specific syntax.
            Self::ILike => "~~*", //Note: This PostgreSQL specific syntax.
            Self::STEVecOperator(op) => op.as_str(),
            // TODO: Probably better to use an Error and handle errors in callers
            _ => "unsupported",
        }
    }
}

impl STEVecOperator {
    pub fn as_str(&self) -> &'static str {
        match self {
            STEVecOperator::LeftContainsRight => "@>",
            STEVecOperator::RightContainsLeft => "<@",
            STEVecOperator::Selector => "->",
            // TODO: implement support for the following (which will require creating a custom PG type called ste_vec with operators defined on it)
            // The above two operators happen to "just work" because cs_ste_vec_v1 is an array and they are defined for arrays and JSON in PG.
            // In the meantime the lack of the following operators can be worked around by converting to `@>` and multiple expressions with logical AND/OR.
            // STEVecOperator::LeftIsTopLevelKey => "?",
            // STEVecOperator::AnyOfLeftIsTopLevelKey => "?|",
            // STEVecOperator::AllOfLeftIsTopLevelKey => "?&",
        }
    }
}

impl FromStr for Operator {
    type Err = InvalidOperatorError;

    fn from_str(op: &str) -> Result<Self, Self::Err> {
        match op {
            "<" => Ok(Self::Lt),
            "<=" => Ok(Self::Lte),
            "=" => Ok(Self::Eq),
            ">" => Ok(Self::Gt),
            ">=" => Ok(Self::Gte),
            "~~" => Ok(Self::Like),
            "~~*" => Ok(Self::ILike),
            "@>" => Ok(Self::STEVecOperator(STEVecOperator::LeftContainsRight)),
            "<@" => Ok(Self::STEVecOperator(STEVecOperator::RightContainsLeft)),
            "->" => Ok(Self::STEVecOperator(STEVecOperator::Selector)),
            invalid => Err(InvalidOperatorError(invalid.to_string())),
        }
    }
}

impl From<&str> for Operator {
    fn from(value: &str) -> Self {
        match value {
            "<" => Self::Lt,
            "<=" => Self::Lte,
            "=" => Self::Eq,
            ">" => Self::Gt,
            ">=" => Self::Gte,
            "~~" => Self::Like,
            "~~*" => Self::ILike,
            "@>" => Self::STEVecOperator(STEVecOperator::LeftContainsRight),
            "<@" => Self::STEVecOperator(STEVecOperator::RightContainsLeft),
            #[allow(deprecated)]
            _ => Self::Unsupported,
        }
    }
}

impl From<String> for Operator {
    fn from(value: String) -> Self {
        value.as_str().into()
    }
}

impl From<Vec<String>> for Operator {
    fn from(value: Vec<String>) -> Self {
        if value.len() == 1 {
            value[0].to_string().into()
        } else {
            #[allow(deprecated)]
            Self::Unsupported
        }
    }
}

impl std::fmt::Display for Operator {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let text = match self {
            Self::Lt => "<",
            Self::Lte => "<=",
            Self::Eq => "==",
            Self::Gt => ">",
            Self::Gte => ">=",
            Self::Like => "LIKE",
            Self::ILike => "ILIKE",
            Self::STEVecOperator(op) => op.as_str(),
            #[allow(deprecated)]
            Self::Unsupported => "Unsupported",
        };

        write!(f, "{text}")
    }
}
