use serde::{Deserialize, Serialize};
use sqlx::FromRow;

#[derive(Debug, FromRow)]
pub struct DbClientConfig {
    pub id: i64,
    pub version: String,
    pub name: String,
    pub stop_server_config_str: String,
    pub client_custom_config_str: String,
    pub dispatch_seed: String,
}

#[derive(Debug, FromRow)]
pub struct DbRegionConfig {
    pub id: i64,
    pub name: String,
    pub title: String,
    pub region_type: String,
    pub area_type: String,
    pub dispatch_url: String,
    pub sdk_env: i32,
    pub client_secret_key: Vec<u8>,
    pub rsa_key_list: String,
}

impl DbRegionConfig {
    pub fn is_key_allowed(&self, key_id: u32) -> bool {
        self.rsa_key_list
            .split(',')
            .map(|s| s.parse::<u32>())
            .flatten()
            .find(|i| key_id == *i)
            .is_some()
    }
}

#[derive(Debug, FromRow)]
pub struct DbBindConfig {
    pub id: i64,
    pub client_version: String,
    pub region_name: String,
    pub channel_id: i32,
}

#[derive(Debug, FromRow)]
pub struct DbChannelIdConfig {
    pub id: i64,
    pub channel_id: i32,
    pub enable_login_pc: bool,
}

#[derive(Debug, FromRow)]
pub struct DbClientRegionConfig {
    pub id: i64,
    pub client_version: String,
    pub region_name: String,
    pub client_region_custom_config: String,
}

#[derive(Debug, FromRow)]
pub struct DbRsakeyConfig {
    pub id: i64,
    pub client_public_key: String,
    pub server_private_key: String,
}

#[derive(Deserialize, Serialize)]
pub struct PlayerExtJsonData {
    pub reg_platform: u32,
}

#[derive(FromRow, Debug)]
pub struct DbPlayerUidRow {
    pub uid: i32,
    pub account_uid: String,
    pub ext: String,
    pub tag: i32,
}

#[derive(FromRow, Debug)]
pub struct DbPlayerDataRow {
    pub uid: i32,
    pub nickname: String,
    pub level: i32,
    pub exp: i32,
    pub vip_point: i32,
    pub json_data: String,
    pub bin_data: Vec<u8>,
    pub extra_bin_data: Option<Vec<u8>>,
    pub tag_list: String,
    pub is_delete: bool,
    pub reserved_1: i32,
    pub reserved_2: i32,
    pub before_login_bin_data: Option<Vec<u8>>,
}
