-- Önce varsa eski tabloları silelim (Sıralama önemli: Foreign key hataları oluşmaması için)

-- 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;

-- Varsayılan Cron ayarını başlat
INSERT INTO `system_settings` (`setting_key`, `setting_value`) VALUES ('last_cron_run', NOW());