// Copyright (C) Call of Nil contributors
// SPDX-License-Identifier: AGPL-3.0-only

use derive_more::{Deref, Display, From, Into};
use diesel::backend::Backend;
use diesel::deserialize::{self as de, FromSql, FromSqlRow};
use diesel::expression::AsExpression;
use diesel::serialize::{self as ser, IsNull, Output, ToSql};
use diesel::sql_types::Text;
use diesel::sqlite::Sqlite;
use nil_core::player::PlayerId;
use std::borrow::Cow;
use std::sync::Arc;

#[derive(
  FromSqlRow, AsExpression, Clone, Debug, Deref, Display, From, Into, PartialEq, Eq, Hash,
)]
#[diesel(sql_type = Text)]
#[from(PlayerId, String, &str, Arc<str>, Box<str>, Cow<'_, str>)]
pub struct db_PlayerId(PlayerId);

impl FromSql<Text, Sqlite> for db_PlayerId {
  fn from_sql(bytes: <Sqlite as Backend>::RawValue<'_>) -> de::Result<Self> {
    let value = <String as FromSql<Text, Sqlite>>::from_sql(bytes)?;
    Ok(db_PlayerId(PlayerId::from(value)))
  }
}

impl ToSql<Text, Sqlite> for db_PlayerId
where
  String: ToSql<Text, Sqlite>,
{
  fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, Sqlite>) -> ser::Result {
    out.set_value(self.0.as_str());
    Ok(IsNull::No)
  }
}

impl From<&PlayerId> for db_PlayerId {
  fn from(id: &PlayerId) -> Self {
    Self(id.clone())
  }
}
