-- Create sessions table for Bearer token authentication
CREATE TABLE IF NOT EXISTS sessions (
    id BIGINT AUTO_INCREMENT PRIMARY KEY,
    session_id_hash VARCHAR(255) NOT NULL UNIQUE,
    user_id INT NOT NULL,
    created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
    expires_at TIMESTAMP NOT NULL,
    INDEX idx_session_hash (session_id_hash),
    INDEX idx_expires_at (expires_at),
    INDEX idx_user_id (user_id),
    INDEX idx_user_created (user_id, created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Create db_users_a table
CREATE TABLE `db_users_a` (
  `id` int NOT NULL AUTO_INCREMENT,
  `screen_name` varchar(75) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '-',
  `telegram_id` bigint UNSIGNED DEFAULT NULL,
  `telegram_name` varchar(33) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT '-',
  `is_premium` int NOT NULL DEFAULT '0',
  `language_code` varchar(2) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT 'XX',
  `country_code` varchar(2) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT 'XX',
  `device_code` varchar(5) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci NOT NULL DEFAULT 'XX',
  `ip` varbinary(16) DEFAULT NULL,
  `date_reg` int NOT NULL DEFAULT '0',
  `date_login` int NOT NULL DEFAULT '0',
  `banned` int NOT NULL DEFAULT '0',
  PRIMARY KEY (`id`),
  UNIQUE KEY `telegram_id` (`telegram_id`),
  KEY `telegram_name` (`telegram_name`),
  KEY `ip` (`ip`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

-- Create db_users_b table
CREATE TABLE `db_users_b` (
  `id` int NOT NULL DEFAULT '0',
  `balance_a` bigint UNSIGNED NOT NULL DEFAULT '0',
  `balance_b` bigint UNSIGNED NOT NULL DEFAULT '0',
  `deposit_total` bigint NOT NULL DEFAULT '0',
  `deposit_count` int NOT NULL DEFAULT '0',
  `withdraw_total` bigint NOT NULL DEFAULT '0',
  `withdraw_count` int NOT NULL DEFAULT '0',
  KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

-- Create db_users_d table
CREATE TABLE `db_users_d` (
  `id` int NOT NULL DEFAULT '0',
  `referer_id_1` int NOT NULL DEFAULT '0',
  `referer_id_2` int NOT NULL DEFAULT '0',
  `referer_id_3` int NOT NULL DEFAULT '0',
  `referer_id_4` int NOT NULL DEFAULT '0',
  `referer_id_5` int NOT NULL DEFAULT '0',
  `master_id` int NOT NULL DEFAULT '0',
  `referals_1` int NOT NULL DEFAULT '0',
  `referals_2` int NOT NULL DEFAULT '0',
  `referals_3` int NOT NULL DEFAULT '0',
  `referals_4` int NOT NULL DEFAULT '0',
  `referals_5` int NOT NULL DEFAULT '0',
  `from_referals_1` bigint NOT NULL DEFAULT '0',
  `from_referals_2` bigint NOT NULL DEFAULT '0',
  `from_referals_3` bigint NOT NULL DEFAULT '0',
  `from_referals_4` bigint NOT NULL DEFAULT '0',
  `from_referals_5` bigint NOT NULL DEFAULT '0',
  `to_referer_1` bigint NOT NULL DEFAULT '0',
  `to_referer_2` bigint NOT NULL DEFAULT '0',
  `to_referer_3` bigint NOT NULL DEFAULT '0',
  `to_referer_4` bigint NOT NULL DEFAULT '0',
  `to_referer_5` bigint NOT NULL DEFAULT '0',
  KEY `id` (`id`),
  KEY `referer_id_1` (`referer_id_1`),
  KEY `referer_id_2` (`referer_id_2`),
  KEY `referer_id_3` (`referer_id_3`),
  KEY `referer_id_4` (`referer_id_4`),
  KEY `referer_id_5` (`referer_id_5`),
  KEY `master_id` (`master_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

-- Add foreign key constraint from sessions to db_users_a
ALTER TABLE `sessions`
  ADD CONSTRAINT `fk_sessions_user_id` FOREIGN KEY (`user_id`) REFERENCES `db_users_a`(`id`) ON DELETE CASCADE;





