<?php
error_reporting(0);
include_once "../config.php";
$db = mysqli_connect('localhost', $databaseUser, $databasePass, $databaseName);
mysqli_multi_query($db, "CREATE TABLE `user` (
  `row` int(11) AUTO_INCREMENT PRIMARY KEY,
  `from_id` varchar(20) COLLATE utf8mb4_bin NOT NULL,
  `step` varchar(50) COLLATE utf8mb4_bin DEFAULT NULL,
  `downloads` int(20) NOT NULL DEFAULT '0',
  `getFile` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL,
  `block` int(11) NOT NULL DEFAULT '0',
  `spam` text COLLATE utf8mb4_bin,
  `update_at` int(20) DEFAULT '0',
  `create_at` int(20) DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;");

mysqli_multi_query($db, "CREATE TABLE `file` (
  `row` int(11) AUTO_INCREMENT PRIMARY KEY,
  `id` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL,
  `type` varchar(50) COLLATE utf8mb4_bin DEFAULT NULL,
  `file_id` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL,
  `caption` text COLLATE utf8mb4_bin,
  `password` varchar(255) COLLATE utf8mb4_bin DEFAULT NULL,
  `user_id` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL,
  `file_size` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL,
  `downloads` int(11) NOT NULL DEFAULT '0',
  `date` char(200) COLLATE utf8mb4_bin DEFAULT NULL,
  `time` char(200) COLLATE utf8mb4_bin DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;");

mysqli_multi_query($db, "CREATE TABLE `lock` (
  `id` int(11) AUTO_INCREMENT PRIMARY KEY,
  `chat_id` varchar(50) COLLATE utf8mb4_bin DEFAULT NULL,
  `link` text COLLATE utf8mb4_bin,
  `count` int(20) NOT NULL DEFAULT '0',
  `type_end` int(11) NOT NULL DEFAULT '0',
  `end` int(20) NOT NULL DEFAULT '0',
  `name` varchar(50) COLLATE utf8mb4_bin DEFAULT NULL,
  `create_at` int(20) DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;");

mysqli_multi_query($db, "CREATE TABLE `send` (
  `row` int(11) AUTO_INCREMENT PRIMARY KEY,
  `step` varchar(50) COLLATE utf8mb4_bin DEFAULT NULL,
  `text` text COLLATE utf8mb4_bin,
  `from` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL,
  `user` int(11) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;");

mysqli_multi_query($db, "CREATE TABLE `settings` (
  `id` int(11) AUTO_INCREMENT PRIMARY KEY,
  `type` varchar(50) COLLATE utf8mb4_bin DEFAULT NULL,
  `type_id` longtext COLLATE utf8mb4_bin,
  `columnOne` longtext COLLATE utf8mb4_bin,
  `columnTwo` longtext COLLATE utf8mb4_bin
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;");

mysqli_multi_query($db, "CREATE TABLE `del` (
  `row` int(11) AUTO_INCREMENT PRIMARY KEY,
  `id` varchar(11) COLLATE utf8mb4_bin DEFAULT NULL,
  `from_id` varchar(15) COLLATE utf8mb4_bin DEFAULT NULL,
  `message_id` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL,
  `timeDel` int(20) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;");

mysqli_multi_query($db, "CREATE TABLE `likes` (
  `id` int(11) AUTO_INCREMENT PRIMARY KEY,
  `file_id` varchar(20) CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL,
  `user_id` bigint(20) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;");

if (mysqli_num_rows($db->query("SELECT `row` FROM `send`")) == 0) $db->query("INSERT INTO `send` (`step`, `text`, `from`, `user`) VALUES('', '', '', 0);");
if (mysqli_num_rows($db->query("SELECT `type` FROM `settings`")) == 0) $db->query("INSERT INTO `settings` (`type`, `type_id`, `columnOne`, `columnTwo`) VALUES ('admin', '$admin', 'main', '');");
if (mysqli_num_rows($db->query("SELECT `type` FROM `settings` WHERE `type` = 'caption'")) == 0) $db->query("INSERT INTO `settings` (`type`, `type_id`, `columnOne`, `columnTwo`) VALUES ('caption', '', '', '');");
if (mysqli_num_rows($db->query("SELECT `type` FROM `settings` WHERE `type` = 'del'")) == 0) $db->query("INSERT INTO `settings` (`type`, `type_id`, `columnOne`, `columnTwo`) VALUES ('del', '', '', '');");
if (mysqli_num_rows($db->query("SELECT `type` FROM `settings` WHERE `type` = 'show_like'")) == 0) $db->query("INSERT INTO `settings` (`type`, `type_id`, `columnOne`, `columnTwo`) VALUES ('show_like', 'active', '', '');");
if (mysqli_num_rows($db->query("SELECT `type` FROM `settings` WHERE `type` = 'show_seen'")) == 0) $db->query("INSERT INTO `settings` (`type`, `type_id`, `columnOne`, `columnTwo`) VALUES ('show_seen', 'active', '', '');");
if (mysqli_num_rows($db->query("SELECT `type` FROM `settings` WHERE `type` = 'bot_status'")) == 0) $db->query("INSERT INTO `settings` (`type`, `type_id`, `columnOne`, `columnTwo`) VALUES ('bot_status', 'on', '', '');");
if (mysqli_num_rows($db->query("SELECT `type` FROM `settings` WHERE `type` = 'start_text'")) == 0) $db->query("INSERT INTO `settings` (`type`, `type_id`, `columnOne`, `columnTwo`) VALUES ('start_text', '', '', '');");
if (mysqli_num_rows($db->query("SELECT `type` FROM `settings` WHERE `type` = 'start_ads'")) == 0) $db->query("INSERT INTO `settings` (`type`, `type_id`, `columnOne`, `columnTwo`) VALUES ('start_ads', '', '', '');");
if (mysqli_num_rows($db->query("SELECT `type` FROM `settings` WHERE `type` = 'post_ads'")) == 0) $db->query("INSERT INTO `settings` (`type`, `type_id`, `columnOne`, `columnTwo`) VALUES ('post_ads', '', '', '');");
if (mysqli_num_rows($db->query("SELECT `type` FROM `settings` WHERE `type` = 'seen_link'")) == 0) $db->query("INSERT INTO `settings` (`type`, `type_id`, `columnOne`, `columnTwo`) VALUES ('seen_link', '', '', '');");
if (mysqli_num_rows($db->query("SELECT `type` FROM `settings` WHERE `type` = 'seen_text'")) == 0) $db->query("INSERT INTO `settings` (`type`, `type_id`, `columnOne`, `columnTwo`) VALUES ('seen_text', '', '', '');");
if (mysqli_num_rows($db->query("SELECT `type` FROM `settings` WHERE `type` = 'seen_time'")) == 0) $db->query("INSERT INTO `settings` (`type`, `type_id`, `columnOne`, `columnTwo`) VALUES ('seen_time', '', '', '');");
if (mysqli_num_rows($db->query("SELECT `type` FROM `settings` WHERE `type` = 'comment_link'")) == 0) $db->query("INSERT INTO `settings` (`type`, `type_id`, `columnOne`, `columnTwo`) VALUES ('comment_link', '', '', '');");
if (mysqli_num_rows($db->query("SELECT `type` FROM `settings` WHERE `type` = 'comment_text'")) == 0) $db->query("INSERT INTO `settings` (`type`, `type_id`, `columnOne`, `columnTwo`) VALUES ('comment_text', '', '', '');");
if (mysqli_num_rows($db->query("SELECT `type` FROM `settings` WHERE `type` = 'lock_text'")) == 0) $db->query("INSERT INTO `settings` (`type`, `type_id`, `columnOne`, `columnTwo`) VALUES ('lock_text', '', '', '');");
if (mysqli_num_rows($db->query("SELECT `type` FROM `settings` WHERE `type` = 'spam_time'")) == 0) $db->query("INSERT INTO `settings` (`type`, `type_id`, `columnOne`, `columnTwo`) VALUES ('spam_time', '', '', '');");
if (mysqli_num_rows($db->query("SELECT `type` FROM `settings` WHERE `type` = 'spam_limit'")) == 0) $db->query("INSERT INTO `settings` (`type`, `type_id`, `columnOne`, `columnTwo`) VALUES ('spam_limit', '', '', '');");
if (mysqli_num_rows($db->query("SELECT `type` FROM `settings` WHERE `type` = 'spoiler'")) == 0) $db->query("INSERT INTO `settings` (`type`, `type_id`, `columnOne`, `columnTwo`) VALUES ('spoiler', 'inactive', '', '');");
if (mysqli_num_rows($db->query("SELECT `type` FROM `settings` WHERE `type` = 'hyperlink'")) == 0) $db->query("INSERT INTO `settings` (`type`, `type_id`, `columnOne`, `columnTwo`) VALUES ('hyperlink', '', '', '');");

// جداول مربوط به سیستم پوشه‌بندی رسانه‌ها
mysqli_multi_query($db, "CREATE TABLE IF NOT EXISTS `folders` (
  `id` int(11) AUTO_INCREMENT PRIMARY KEY,
  `name` varchar(255) COLLATE utf8mb4_bin NOT NULL,
  `parent_id` int(11) DEFAULT NULL,
  `order_position` int(11) DEFAULT 0,
  `created_at` int(20) DEFAULT 0,
  `created_by` varchar(20) COLLATE utf8mb4_bin DEFAULT NULL,
  FOREIGN KEY (`parent_id`) REFERENCES `folders`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;");

mysqli_multi_query($db, "CREATE TABLE IF NOT EXISTS `folder_files` (
  `id` int(11) AUTO_INCREMENT PRIMARY KEY,
  `folder_id` int(11) NOT NULL,
  `file_id` varchar(20) COLLATE utf8mb4_bin NOT NULL,
  `created_at` int(20) DEFAULT 0,
  FOREIGN KEY (`folder_id`) REFERENCES `folders`(`id`) ON DELETE CASCADE,
  FOREIGN KEY (`file_id`) REFERENCES `file`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;");

// تنظیمات مربوط به پوشه‌بندی
if (mysqli_num_rows($db->query("SELECT `type` FROM `settings` WHERE `type` = 'folders_enabled'")) == 0) $db->query("INSERT INTO `settings` (`type`, `type_id`, `columnOne`, `columnTwo`) VALUES ('folders_enabled', 'on', '', '');");