//------------------------------------------------------------------------------
// InsertValue Definition - SQL-based value for inserts
//------------------------------------------------------------------------------

use core::marker::PhantomData;

use crate::prelude::*;
use drizzle_core::{Placeholder, SQL, SQLParam, TypedPlaceholder};

use super::{OwnedSQLiteValue, SQLiteValue};

#[doc(hidden)]
#[derive(Debug, Clone)]
pub struct ValueWrapper<'a, V: SQLParam, T> {
    pub value: SQL<'a, V>,
    pub _phantom: PhantomData<T>,
}

impl<'a, V: SQLParam, T> ValueWrapper<'a, V, T> {
    pub const fn new<U>(value: SQL<'a, V>) -> ValueWrapper<'a, V, U> {
        ValueWrapper {
            value,
            _phantom: PhantomData,
        }
    }
}

/// Represents a value for INSERT operations that can be omitted, null, or a SQL expression
#[derive(Debug, Clone, Default)]
#[allow(clippy::large_enum_variant)]
pub enum SQLiteInsertValue<'a, V: SQLParam, T> {
    /// Omit this column from the INSERT (use database default)
    #[default]
    Omit,
    /// Explicitly insert NULL
    Null,
    /// Insert a SQL expression (value, placeholder, etc.)
    Value(ValueWrapper<'a, V, T>),
}

impl<'a, T> SQLiteInsertValue<'a, SQLiteValue<'a>, T> {
    /// Converts this `InsertValue` to an owned version with 'static lifetime
    #[must_use]
    pub fn into_owned(self) -> SQLiteInsertValue<'static, SQLiteValue<'static>, T> {
        match self {
            SQLiteInsertValue::Omit => SQLiteInsertValue::Omit,
            SQLiteInsertValue::Null => SQLiteInsertValue::Null,
            SQLiteInsertValue::Value(wrapper) => {
                // Extract the parameter value, convert to owned, then back to static SQLiteValue
                let static_sql = match wrapper.value.chunks.first() {
                    Some(drizzle_core::SQLChunk::Param(param)) => param.value.as_ref().map_or_else(
                        || drizzle_core::SQL::param(SQLiteValue::Null),
                        |val| {
                            let owned_val = OwnedSQLiteValue::from(val.as_ref().clone());
                            let static_val: SQLiteValue<'static> = owned_val.into();
                            drizzle_core::SQL::param(static_val)
                        },
                    ),
                    _ => drizzle_core::SQL::param(SQLiteValue::Null),
                };
                SQLiteInsertValue::Value(ValueWrapper::<SQLiteValue<'static>, T>::new(static_sql))
            }
        }
    }
}

impl<'a, T, U> From<T> for SQLiteInsertValue<'a, SQLiteValue<'a>, U>
where
    T: TryInto<SQLiteValue<'a>> + TryInto<U>,
    U: TryInto<SQLiteValue<'a>>,
{
    fn from(value: T) -> Self {
        let sql = TryInto::<U>::try_into(value)
            .map(|v| v.try_into().unwrap_or_default())
            .map_or_else(
                |_| SQL::from(SQLiteValue::Null),
                |v: SQLiteValue<'a>| SQL::from(v),
            );
        SQLiteInsertValue::Value(ValueWrapper::<SQLiteValue<'a>, T>::new(sql))
    }
}

impl<'a, T> From<Placeholder> for SQLiteInsertValue<'a, SQLiteValue<'a>, T> {
    fn from(placeholder: Placeholder) -> Self {
        use drizzle_core::{Param, SQLChunk};
        let chunk = SQLChunk::Param(Param {
            placeholder,
            value: None,
        });
        SQLiteInsertValue::Value(ValueWrapper::<SQLiteValue<'a>, T>::new(
            core::iter::once(chunk).collect(),
        ))
    }
}

impl<'a, M: drizzle_core::types::DataType, N: drizzle_core::expr::Nullability, T>
    From<TypedPlaceholder<M, N>> for SQLiteInsertValue<'a, SQLiteValue<'a>, T>
{
    fn from(typed: TypedPlaceholder<M, N>) -> Self {
        Placeholder::from(typed).into()
    }
}

// Array conversion for Vec<u8> InsertValue - support flexible input types
impl<'a, const N: usize> From<[u8; N]> for SQLiteInsertValue<'a, SQLiteValue<'a>, Vec<u8>> {
    fn from(value: [u8; N]) -> Self {
        let sqlite_value = SQLiteValue::Blob(Cow::Owned(value.to_vec()));
        let sql = SQL::param(sqlite_value);
        SQLiteInsertValue::Value(ValueWrapper::<SQLiteValue<'a>, Vec<u8>>::new(sql))
    }
}
