-- Önce varsa eski tabloları silelim (Sıralama önemli: Foreign key hataları oluşmaması için)
SET FOREIGN_KEY_CHECKS = 0;
DROP TABLE IF EXISTS `process_logs`;
DROP TABLE IF EXISTS `bots`;
DROP TABLE IF EXISTS `vds_nodes`;
DROP TABLE IF EXISTS `customer_accounts`;
DROP TABLE IF EXISTS `dealer_accounts`;
DROP TABLE IF EXISTS `admin_accounts`;
DROP TABLE IF EXISTS `system_settings`;
SET FOREIGN_KEY_CHECKS = 1;

-- 1. ADMİNLER TABLOSU
CREATE TABLE `admin_accounts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(50) NOT NULL UNIQUE,
  `password` varchar(255) NOT NULL,
  `email` varchar(100) DEFAULT NULL,
  `failed_login_attempts` int(11) DEFAULT 0,
  `last_failed_login` datetime DEFAULT NULL,
  `last_process_time` timestamp NULL DEFAULT NULL, 
  `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 2. BAYİLER (DEALERS) TABLOSU
CREATE TABLE `dealer_accounts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `admin_id` int(11) NOT NULL,
  `username` varchar(50) NOT NULL UNIQUE,
  `password` varchar(255) NOT NULL,
  `max_bot_limit` int(11) DEFAULT 10,
  `max_port_limit` int(11) DEFAULT 10,
  `can_add_vds` tinyint(1) DEFAULT 0,
  `status` tinyint(1) DEFAULT 1,
  `failed_login_attempts` int(11) DEFAULT 0,
  `last_failed_login` datetime DEFAULT NULL,
  `last_process_time` timestamp NULL DEFAULT NULL,
  `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  CONSTRAINT `fk_dealer_admin` FOREIGN KEY (`admin_id`) REFERENCES `admin_accounts` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 3. MÜŞTERİLER (CUSTOMERS) TABLOSU
CREATE TABLE `customer_accounts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `dealer_id` int(11) DEFAULT NULL,
  `admin_id` int(11) DEFAULT NULL,
  `username` varchar(50) NOT NULL UNIQUE,
  `password` varchar(255) NOT NULL,
  `status` tinyint(1) DEFAULT 1,
  `failed_login_attempts` int(11) DEFAULT 0,
  `last_failed_login` datetime DEFAULT NULL,
  `last_process_time` timestamp NULL DEFAULT NULL,
  `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  CONSTRAINT `fk_customer_dealer` FOREIGN KEY (`dealer_id`) REFERENCES `dealer_accounts` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 4. VDS SUNUCULARI
CREATE TABLE `vds_nodes` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `creator_id` int(11) NOT NULL,
  `creator_role` enum('admin','bayi') NOT NULL,
  `node_name` varchar(100) NOT NULL,
  `ip_address` varchar(45) NOT NULL,
  `api_port` int(11) DEFAULT 58913,
  `status` tinyint(1) DEFAULT 1,
  `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 5. BOTLAR VE SÜRE TAKİBİ
CREATE TABLE `bots` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `customer_id` int(11) NOT NULL,
  `vds_id` int(11) NOT NULL,
  `bot_uid` varchar(100) NOT NULL UNIQUE,
  `assigned_port` int(11) DEFAULT NULL,
  `start_date` datetime DEFAULT CURRENT_TIMESTAMP,
  `expire_date` datetime NOT NULL,
  `status` enum('active','suspended','expired') DEFAULT 'active',
  `last_cron_check` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  CONSTRAINT `fk_bot_customer` FOREIGN KEY (`customer_id`) REFERENCES `customer_accounts` (`id`) ON DELETE CASCADE,
  CONSTRAINT `fk_bot_vds` FOREIGN KEY (`vds_id`) REFERENCES `vds_nodes` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 6. İŞLEM LOGLARI
CREATE TABLE `process_logs` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `performer_id` int(11) NOT NULL,
  `performer_role` enum('admin','bayi','musteri') NOT NULL,
  `action_text` text NOT NULL,
  `ip_address` varchar(45) DEFAULT NULL,
  `is_impersonated` tinyint(1) DEFAULT 0,
  `created_at` timestamp DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- 7. SİSTEM AYARLARI
CREATE TABLE `system_settings` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `setting_key` varchar(50) NOT NULL UNIQUE,
  `setting_value` text,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- ############################################################
-- HAZIR ADMİN HESABI EKLEME
-- Kullanıcı Adı: admin
-- Şifre: 123456
-- ############################################################

INSERT INTO `admin_accounts` (`username`, `password`, `email`) 
VALUES ('admin', '$2y$10$8WlS99F9q6F.CpsS3uL/..zT5fM6P.qU7KkY5n/E0A1/V1Z2m6W6S', 'admin@panel.com');

-- Varsayılan Cron ayarını başlat
INSERT INTO `system_settings` (`setting_key`, `setting_value`) VALUES ('last_cron_run', NOW());