create function unhex(text) returns text language sql immutable strict as $$
  select encode(decode($1, 'hex'), 'escape')
$$;

CREATE TABLE t_sdk_account (
    uid int primary key generated always as identity,
    token varchar(64) NOT NULL,
    username varchar(40) NOT NULL,
    password varchar(256) NOT NULL,
    UNIQUE(username)
);

CREATE TABLE t_combo_token (
    account_uid varchar(32) primary key,
    token varchar(64) NOT NULL,
    device_id varchar(128) NOT NULL,
    UNIQUE(account_uid, device_id)
);

CREATE TABLE t_client_config (
	id bigint primary key,
	version varchar(64) NOT NULL DEFAULT '',
	name varchar(64) NOT NULL DEFAULT '',
	stop_server_config_str varchar(512) NOT NULL DEFAULT '',
	client_custom_config_str varchar(512) NOT NULL DEFAULT '',
	dispatch_seed varchar(512) NOT NULL DEFAULT '',
	UNIQUE(version)
);

INSERT INTO t_client_config VALUES
(1, 'CNCBWin5.0.50',  '', '', '{\"codeSwitch\":[4334],\"sdkenv\":2,\"checkdevice\":false,\"loadPatch\":false,\"showexception\":false,\"regionConfig\":\"pm|fk|add\",\"downloadMode\":0}', '');

CREATE TABLE t_region_config (
	id bigint primary key,
	name varchar(64) NOT NULL DEFAULT '',
	title varchar(128) NOT NULL DEFAULT '',
	region_type varchar(64) NOT NULL DEFAULT '',
	area_type varchar(64) NOT NULL DEFAULT '',
	dispatch_url varchar(64) NOT NULL DEFAULT '',
	sdk_env int NOT NULL,
	client_secret_key bytea NOT NULL DEFAULT '',
	rsa_key_list varchar(128) NOT NULL DEFAULT '',
	UNIQUE(name)
);

INSERT INTO t_region_config VALUES
(1, 'dev_xilonen', 'XilonenImpact', 'DEV_PUBLIC', 'global', 'http://127.0.0.1:21041/query_cur_region', 2, decode(' ', 'hex'), '4,5');

CREATE TABLE t_bind_config (
	id bigint primary key,
	client_version varchar(64) NOT NULL,
	region_name varchar(64) NOT NULL,
	channel_id int NOT NULL
);

INSERT INTO t_bind_config VALUES
(1, 'CNCBWin5.0.50', 'dev_xilonen', 1);

CREATE TABLE t_channel_id_config (
	id bigint primary key,
	channel_id int NOT NULL,
	enable_login_pc bool
);

INSERT INTO t_channel_id_config VALUES
(1, 1, TRUE);

CREATE TABLE t_client_region_config (
	id bigint primary key,
	client_version varchar(64) NOT NULL,
	region_name varchar(64) NOT NULL,
	client_region_custom_config varchar(512) NOT NULL
);

INSERT INTO t_client_region_config VALUES
(1, 'CNCBWin5.0.50', 'dev_xilonen', '');

CREATE TABLE t_rsakey_config (
	id bigint primary key,
	client_public_key varchar(4096) NOT NULL,
	server_private_key varchar(4096) NOT NULL
);

INSERT INTO t_rsakey_config VALUES
(4, E'-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAyaxqjPJP5+Innfv5IdfQ\nqY/ftS++lnDRe3EczNkIjESWXhHSOljEw9b9C+/BtF+fO9QZL7Z742y06eIdvsMP\nQKdGflB26+9OZ8AF4SpXDn3aVWGr8+9qpB7BELRZI/Ph2FlFL4cobCzMHunncW8z\nTfMId48+fgHkAzCjRl5rC6XT0Yge6+eKpXmF+hr0vGYWiTzqPzTABl44WZo3rw0y\nurZTzkrmRE4kR2VzkjY/rBnQAbFKKFUKsUozjCXvSag4l461wDkhmmyivpNkK5cA\nxuDbsmC39iqagMt9438fajLVvYOvpVs9ci5tiLcbBtfB4Rf/QVAkqtTm86Z0O3e7\nDwIDAQAB\n-----END PUBLIC KEY-----', E'-----BEGIN RSA PRIVATE KEY-----\nMIIEpQIBAAKCAQEAxbbx2m1feHyrQ7jP+8mtDF/pyYLrJWKWAdEv3wZrOtjOZzeL\nGPzsmkcgncgoRhX4dT+1itSMR9j9m0/OwsH2UoF6U32LxCOQWQD1AMgIZjAkJeJv\nFTrtn8fMQ1701CkbaLTVIjRMlTw8kNXvNA/A9UatoiDmi4TFG6mrxTKZpIcTInvP\nEpkK2A7Qsp1E4skFK8jmysy7uRhMaYHtPTsBvxP0zn3lhKB3W+HTqpneewXWHjCD\nfL7Nbby91jbz5EKPZXWLuhXIvR1Cu4tiruorwXJxmXaP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DsoTzGs7bcdMl72wvKemRaU92ckMEZpzAT8cEMC\ncWKLb8yzAoGAMibG8IyHSo7CJz82+7UHm98jNOlg6s73CEjp0W/+FL45Ka7MF/lp\nxtR3eSmxltvwvjQoti3V4Qboqtc2IPCt+EtapTM7Wo41wlLCWCNx4u25pZPH/c8g\n1yQ+OvH+xOYG+SeO98Phw/8d3IRfR83aqisQHv5upo2Rozzo0Kh3OsE=\n-----END RSA PRIVATE KEY-----');

CREATE TABLE t_user_uid (
	uid int primary key NOT NULL,
	account_uid varchar(128) NOT NULL,
	ext varchar(512) NOT NULL DEFAULT '',
	tag int NOT NULL DEFAULT 0,
	UNIQUE(account_uid)
);

CREATE TABLE t_player_data (
	uid int primary key,
	nickname varchar(128) NOT NULL DEFAULT '0',
	level int NOT NULL DEFAULT 0,
	exp int NOT NULL DEFAULT 0,
	vip_point int NOT NULL DEFAULT 0,
	json_data varchar(512) NOT NULL DEFAULT '',
	bin_data bytea NOT NULL,
	extra_bin_data bytea DEFAULT NULL,
	tag_list varchar(128) NOT NULL DEFAULT '',
	is_delete bool,
	reserved_1 int,
	reserved_2 int,
	before_login_bin_data bytea DEFAULT NULL
);

