-- =====================================================================
--  Telegram Encoder Suite — Complete MySQL Schema
--  Single source of truth for BOTH the PHP Telegram Bot and PHP Admin.
--  MySQL 5.7+ / MariaDB 10.3+  (utf8mb4)
--
--  How to install on cPanel:
--    1. Create a MySQL database + user in cPanel (Databases > MySQL).
--    2. Open phpMyAdmin, select the database, go to "Import".
--    3. Upload this file. Done — all tables + defaults are created.
--
--  Default admin login (CHANGE IMMEDIATELY after first login):
--    username: admin
--    password: Admin@12345
-- =====================================================================

SET NAMES utf8mb4;
SET FOREIGN_KEY_CHECKS = 0;

-- ---------------------------------------------------------------------
--  roles
-- ---------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `roles` (
  `id`          INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `name`        VARCHAR(50)  NOT NULL,
  `slug`        VARCHAR(50)  NOT NULL,
  `permissions` TEXT         NULL COMMENT 'JSON array of permission slugs, or ["*"] for all',
  `created_at`  TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_role_slug` (`slug`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ---------------------------------------------------------------------
--  admins
-- ---------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `admins` (
  `id`             INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `role_id`        INT UNSIGNED NOT NULL DEFAULT 1,
  `username`       VARCHAR(64)  NOT NULL,
  `full_name`      VARCHAR(120) NULL,
  `email`          VARCHAR(160) NULL,
  `password_hash`  VARCHAR(255) NOT NULL,
  `is_active`      TINYINT(1)   NOT NULL DEFAULT 1,
  `failed_logins`  INT UNSIGNED NOT NULL DEFAULT 0,
  `locked_until`   TIMESTAMP    NULL,
  `last_login_at`  TIMESTAMP    NULL,
  `last_login_ip`  VARCHAR(45)  NULL,
  `created_at`     TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at`     TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_admin_username` (`username`),
  KEY `idx_admin_role` (`role_id`),
  CONSTRAINT `fk_admin_role` FOREIGN KEY (`role_id`) REFERENCES `roles` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ---------------------------------------------------------------------
--  plans  (free / paid / premium — all driven from here)
-- ---------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `plans` (
  `id`             INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `name`           VARCHAR(80)  NOT NULL,
  `type`           ENUM('free','paid','premium') NOT NULL DEFAULT 'free',
  `is_active`      TINYINT(1)   NOT NULL DEFAULT 1,
  `price`          DECIMAL(10,2) NOT NULL DEFAULT 0.00,
  `currency`       VARCHAR(8)   NOT NULL DEFAULT 'USD',
  `duration_days`  INT          NOT NULL DEFAULT 0 COMMENT '0 = no expiry',
  `encode_limit`   INT          NOT NULL DEFAULT 5   COMMENT '-1 = unlimited',
  `decode_limit`   INT          NOT NULL DEFAULT 5   COMMENT '-1 = unlimited',
  `daily_limit`    INT          NOT NULL DEFAULT 20  COMMENT '-1 = unlimited',
  `monthly_limit`  INT          NOT NULL DEFAULT 300 COMMENT '-1 = unlimited',
  `storage_limit`  BIGINT       NOT NULL DEFAULT 52428800  COMMENT 'bytes, -1 = unlimited',
  `file_size_limit` BIGINT      NOT NULL DEFAULT 5242880   COMMENT 'bytes per file',
  `cooldown_sec`   INT          NOT NULL DEFAULT 15,
  `allowed_tools`  TEXT         NULL COMMENT 'JSON array of tool slugs',
  `is_unlimited`   TINYINT(1)   NOT NULL DEFAULT 0,
  `sort_order`     INT          NOT NULL DEFAULT 0,
  `created_at`     TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `updated_at`     TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_plan_type` (`type`),
  KEY `idx_plan_active` (`is_active`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ---------------------------------------------------------------------
--  users  (Telegram users — created by the bot)
-- ---------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `users` (
  `id`              BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `telegram_id`     BIGINT       NOT NULL,
  `chat_id`         BIGINT       NOT NULL,
  `username`        VARCHAR(64)  NULL,
  `first_name`      VARCHAR(120) NULL,
  `last_name`       VARCHAR(120) NULL,
  `language_code`   VARCHAR(12)  NULL,
  `photo_url`       VARCHAR(255) NULL,
  `plan_id`         INT UNSIGNED NULL,
  `is_paid`         TINYINT(1)   NOT NULL DEFAULT 0,
  `is_premium`      TINYINT(1)   NOT NULL DEFAULT 0,
  `is_banned`       TINYINT(1)   NOT NULL DEFAULT 0,
  `expiry_at`       TIMESTAMP    NULL,
  `storage_used`    BIGINT       NOT NULL DEFAULT 0,
  `state`           VARCHAR(64)  NULL COMMENT 'current conversation state e.g. await_encode',
  `state_data`      TEXT         NULL COMMENT 'JSON temp data for current flow',
  `joined_at`       TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `last_activity_at` TIMESTAMP   NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_user_tg` (`telegram_id`),
  KEY `idx_user_username` (`username`),
  KEY `idx_user_chat` (`chat_id`),
  KEY `idx_user_plan` (`plan_id`),
  KEY `idx_user_flags` (`is_banned`,`is_paid`,`is_premium`),
  CONSTRAINT `fk_user_plan` FOREIGN KEY (`plan_id`) REFERENCES `plans` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ---------------------------------------------------------------------
--  subscriptions
-- ---------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `subscriptions` (
  `id`          BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id`     BIGINT UNSIGNED NOT NULL,
  `plan_id`     INT UNSIGNED NOT NULL,
  `status`      ENUM('active','expired','cancelled','pending') NOT NULL DEFAULT 'active',
  `start_at`    TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `expiry_at`   TIMESTAMP    NULL,
  `is_paid`     TINYINT(1)   NOT NULL DEFAULT 0,
  `is_premium`  TINYINT(1)   NOT NULL DEFAULT 0,
  `activated_by` INT UNSIGNED NULL COMMENT 'admin id (manual activation)',
  `note`        VARCHAR(255) NULL,
  `created_at`  TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_sub_user` (`user_id`),
  KEY `idx_sub_plan` (`plan_id`),
  KEY `idx_sub_status` (`status`),
  CONSTRAINT `fk_sub_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
  CONSTRAINT `fk_sub_plan` FOREIGN KEY (`plan_id`) REFERENCES `plans` (`id`) ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ---------------------------------------------------------------------
--  tools  (each bot tool, toggled entirely from admin)
-- ---------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `tools` (
  `id`             INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `slug`           VARCHAR(64)  NOT NULL,
  `name`           VARCHAR(120) NOT NULL,
  `description`    VARCHAR(255) NULL,
  `is_enabled`     TINYINT(1)   NOT NULL DEFAULT 1,
  `free_access`    TINYINT(1)   NOT NULL DEFAULT 1,
  `paid_access`    TINYINT(1)   NOT NULL DEFAULT 1,
  `premium_access` TINYINT(1)   NOT NULL DEFAULT 1,
  `usage_limit`    INT          NOT NULL DEFAULT -1 COMMENT '-1 = follow plan',
  `cooldown_sec`   INT          NOT NULL DEFAULT 0  COMMENT '0 = follow plan',
  `maintenance`    TINYINT(1)   NOT NULL DEFAULT 0,
  `sort_order`     INT          NOT NULL DEFAULT 0,
  `updated_at`     TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_tool_slug` (`slug`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ---------------------------------------------------------------------
--  tool_usage  (per user / per tool counters)
-- ---------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `tool_usage` (
  `id`           BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id`      BIGINT UNSIGNED NOT NULL,
  `tool_slug`    VARCHAR(64)  NOT NULL,
  `total_count`  INT UNSIGNED NOT NULL DEFAULT 0,
  `daily_count`  INT UNSIGNED NOT NULL DEFAULT 0,
  `monthly_count` INT UNSIGNED NOT NULL DEFAULT 0,
  `daily_date`   DATE         NULL,
  `monthly_ym`   CHAR(7)      NULL COMMENT 'YYYY-MM',
  `last_used_at` TIMESTAMP    NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_usage` (`user_id`,`tool_slug`),
  KEY `idx_usage_tool` (`tool_slug`),
  CONSTRAINT `fk_usage_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ---------------------------------------------------------------------
--  user_activity
-- ---------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `user_activity` (
  `id`         BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id`    BIGINT UNSIGNED NOT NULL,
  `action`     VARCHAR(80)  NOT NULL,
  `detail`     VARCHAR(255) NULL,
  `created_at` TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_activity_user` (`user_id`),
  KEY `idx_activity_time` (`created_at`),
  CONSTRAINT `fk_activity_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ---------------------------------------------------------------------
--  encode_history
-- ---------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `encode_history` (
  `id`            BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id`       BIGINT UNSIGNED NOT NULL,
  `file_name`     VARCHAR(255) NULL,
  `file_size`     BIGINT       NOT NULL DEFAULT 0,
  `output_size`   BIGINT       NOT NULL DEFAULT 0,
  `algorithm`     VARCHAR(64)  NULL,
  `layers`        VARCHAR(120) NULL,
  `protection_level` VARCHAR(32) NULL,
  `processing_ms` INT          NOT NULL DEFAULT 0,
  `status`        ENUM('success','failed') NOT NULL DEFAULT 'success',
  `error`         VARCHAR(255) NULL,
  `stored_file_id` BIGINT UNSIGNED NULL,
  `created_at`    TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_enc_user` (`user_id`),
  KEY `idx_enc_status` (`status`),
  KEY `idx_enc_time` (`created_at`),
  CONSTRAINT `fk_enc_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ---------------------------------------------------------------------
--  decode_history
-- ---------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `decode_history` (
  `id`            BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id`       BIGINT UNSIGNED NOT NULL,
  `file_name`     VARCHAR(255) NULL,
  `file_size`     BIGINT       NOT NULL DEFAULT 0,
  `output_size`   BIGINT       NOT NULL DEFAULT 0,
  `algorithm`     VARCHAR(64)  NULL,
  `layers`        VARCHAR(120) NULL,
  `processing_ms` INT          NOT NULL DEFAULT 0,
  `status`        ENUM('success','failed') NOT NULL DEFAULT 'success',
  `error`         VARCHAR(255) NULL,
  `stored_file_id` BIGINT UNSIGNED NULL,
  `created_at`    TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_dec_user` (`user_id`),
  KEY `idx_dec_status` (`status`),
  KEY `idx_dec_time` (`created_at`),
  CONSTRAINT `fk_dec_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ---------------------------------------------------------------------
--  files  (uploaded + generated, unified with a direction flag)
-- ---------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `files` (
  `id`           BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `user_id`      BIGINT UNSIGNED NULL,
  `kind`         ENUM('uploaded','generated') NOT NULL,
  `tool_slug`    VARCHAR(64)  NULL,
  `original_name` VARCHAR(255) NULL,
  `stored_name`  VARCHAR(255) NOT NULL COMMENT 'random unique on-disk name',
  `mime`         VARCHAR(120) NULL,
  `size`         BIGINT       NOT NULL DEFAULT 0,
  `sha256`       CHAR(64)     NULL,
  `created_at`   TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_file_user` (`user_id`),
  KEY `idx_file_kind` (`kind`),
  UNIQUE KEY `uniq_stored` (`stored_name`),
  CONSTRAINT `fk_file_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ---------------------------------------------------------------------
--  bot_settings  (key/value — global config)
-- ---------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `bot_settings` (
  `id`          INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `skey`        VARCHAR(80)  NOT NULL,
  `svalue`      TEXT         NULL,
  `updated_at`  TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_setting` (`skey`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ---------------------------------------------------------------------
--  bot_messages  (all editable bot texts)
-- ---------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `bot_messages` (
  `id`          INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `mkey`        VARCHAR(80)  NOT NULL,
  `title`       VARCHAR(120) NULL,
  `content`     TEXT         NULL,
  `updated_at`  TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_message` (`mkey`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ---------------------------------------------------------------------
--  premium_emojis
-- ---------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `premium_emojis` (
  `id`           INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `label`        VARCHAR(80)  NOT NULL,
  `placeholder`  VARCHAR(40)  NOT NULL COMMENT 'e.g. {star}',
  `fallback`     VARCHAR(16)  NULL COMMENT 'plain emoji fallback',
  `custom_emoji_id` VARCHAR(40) NOT NULL COMMENT 'Telegram custom_emoji_id',
  `is_active`    TINYINT(1)   NOT NULL DEFAULT 1,
  `updated_at`   TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  UNIQUE KEY `uniq_placeholder` (`placeholder`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ---------------------------------------------------------------------
--  broadcast_history
-- ---------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `broadcast_history` (
  `id`           BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `admin_id`     INT UNSIGNED NULL,
  `target`       VARCHAR(32)  NOT NULL COMMENT 'all|free|paid|premium|selected',
  `message`      TEXT         NULL,
  `total`        INT          NOT NULL DEFAULT 0,
  `success`      INT          NOT NULL DEFAULT 0,
  `failed`       INT          NOT NULL DEFAULT 0,
  `status`       ENUM('pending','running','done','failed') NOT NULL DEFAULT 'pending',
  `created_at`   TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `finished_at`  TIMESTAMP    NULL,
  PRIMARY KEY (`id`),
  KEY `idx_bc_admin` (`admin_id`),
  KEY `idx_bc_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ---------------------------------------------------------------------
--  admin_activity_logs
-- ---------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `admin_activity_logs` (
  `id`         BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,
  `admin_id`   INT UNSIGNED NULL,
  `admin_name` VARCHAR(120) NULL,
  `action`     VARCHAR(120) NOT NULL,
  `target`     VARCHAR(160) NULL,
  `ip_address` VARCHAR(45)  NULL,
  `user_agent` VARCHAR(255) NULL,
  `result`     ENUM('success','failed') NOT NULL DEFAULT 'success',
  `created_at` TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_log_admin` (`admin_id`),
  KEY `idx_log_time` (`created_at`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- ---------------------------------------------------------------------
--  force_channels  (Force Join / Force Subscribe)
--  The bot must be an ADMIN of each channel so it can verify membership.
-- ---------------------------------------------------------------------
CREATE TABLE IF NOT EXISTS `force_channels` (
  `id`          INT UNSIGNED NOT NULL AUTO_INCREMENT,
  `title`       VARCHAR(120) NOT NULL,
  `join_url`    VARCHAR(255) NOT NULL COMMENT 'button link, e.g. https://t.me/yourchannel',
  `check_id`    VARCHAR(120) NOT NULL COMMENT '@username (public) or -100... chat id (private)',
  `is_active`   TINYINT(1)   NOT NULL DEFAULT 1,
  `sort_order`  INT          NOT NULL DEFAULT 0,
  `created_at`  TIMESTAMP    NOT NULL DEFAULT CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`),
  KEY `idx_fc_active` (`is_active`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- =====================================================================
--  SEED DATA
-- =====================================================================

-- Roles
INSERT INTO `roles` (`id`,`name`,`slug`,`permissions`) VALUES
  (1,'Super Admin','super_admin','["*"]'),
  (2,'Admin','admin','["dashboard","users","plans","subscriptions","files","tools","broadcast","content","statistics","logs","settings","forcejoin"]'),
  (3,'Manager','manager','["dashboard","users","plans","subscriptions","files","content","statistics","forcejoin"]'),
  (4,'Viewer','viewer','["dashboard","users","statistics","logs"]')
ON DUPLICATE KEY UPDATE `name`=VALUES(`name`);

-- Default admin: password = Admin@12345  (bcrypt hash below)
INSERT INTO `admins` (`id`,`role_id`,`username`,`full_name`,`email`,`password_hash`,`is_active`) VALUES
  (1,1,'admin','Super Admin','admin@example.com',
   '$2y$10$KQ327VECEcuJUJpHIAGh/e7yaa1BlkrmLWCP6urHSbPgULHKzKmee', 1)
ON DUPLICATE KEY UPDATE `username`=VALUES(`username`);

-- Plans
INSERT INTO `plans`
  (`id`,`name`,`type`,`is_active`,`price`,`currency`,`duration_days`,`encode_limit`,`decode_limit`,`daily_limit`,`monthly_limit`,`storage_limit`,`file_size_limit`,`cooldown_sec`,`allowed_tools`,`is_unlimited`,`sort_order`)
VALUES
  (1,'Free','free',1,0.00,'USD',0,5,5,20,300,52428800,5242880,15,
     '["html_encode","html_decode","weblink_zip","url_html"]',0,1),
  (2,'Paid Monthly','paid',1,4.99,'USD',30,100,100,500,10000,524288000,26214400,3,
     '["html_encode","html_decode","weblink_zip","url_html"]',0,2),
  (3,'Premium','premium',1,9.99,'USD',30,-1,-1,-1,-1,-1,52428800,0,
     '["html_encode","html_decode","weblink_zip","url_html"]',1,3)
ON DUPLICATE KEY UPDATE `name`=VALUES(`name`);

-- Tools
INSERT INTO `tools` (`slug`,`name`,`description`,`is_enabled`,`free_access`,`paid_access`,`premium_access`,`sort_order`) VALUES
  ('html_encode','HTML Encode','Protect an HTML file with the 5-layer engine',1,1,1,1,1),
  ('html_decode','HTML Decode','Restore a protected HTML file (password required)',1,1,1,1,2),
  ('weblink_zip','Web Link → Source ZIP','Download a page + its assets as a ZIP',1,1,1,1,3),
  ('url_html','URL → HTML','Fetch a URL and return the raw HTML file',1,1,1,1,4)
ON DUPLICATE KEY UPDATE `name`=VALUES(`name`);

-- Bot settings (global config)
INSERT INTO `bot_settings` (`skey`,`svalue`) VALUES
  ('bot_name','Encoder Suite Bot'),
  ('bot_username',''),
  ('owner_id','0'),
  ('default_plan_id','1'),
  ('file_retention_days','7'),
  ('max_file_size','5242880'),
  ('global_maintenance','0'),
  ('registration_open','1'),
  ('default_encrypt_algo','aes-256-gcm'),
  ('kdf','pbkdf2'),
  ('kdf_iterations','200000'),
  ('bot_token',''),
  ('webhook_secret',''),
  ('force_join_enabled','0'),
  ('force_join_message','⚠️ Please join our channel(s) below, then tap ✅ I have joined to continue.'),
  ('force_join_media_type','none'),
  ('force_join_media','')
ON DUPLICATE KEY UPDATE `svalue`=`svalue`;

-- Bot messages (editable text)
INSERT INTO `bot_messages` (`mkey`,`title`,`content`) VALUES
  ('start','Start Message','👋 Welcome {first_name}!\n\nI can protect and restore HTML files.\n\nUse the buttons below to pick a tool.'),
  ('help','Help Message','ℹ️ *Help*\n\n• HTML Encode — protect an .html file (5 layers)\n• HTML Decode — restore a protected file (needs password)\n• Web Link → ZIP — download a page + assets\n• URL → HTML — fetch a page as raw HTML\n\nSend /start to see the menu.'),
  ('maintenance','Maintenance','🛠 The bot is under maintenance. Please try again later.'),
  ('banned','Banned','🚫 Your access has been suspended. Contact the administrator.'),
  ('registration_closed','Registration Closed','⛔ New registrations are currently closed.'),
  ('limit_reached','Limit Reached','⚠️ You have reached your usage limit for this tool. Upgrade your plan or try again later.'),
  ('cooldown','Cooldown','⏳ Please wait {seconds}s before using this tool again.'),
  ('expired','Subscription Expired','⌛ Your subscription has expired. You are back on the Free plan.'),
  ('tool_disabled','Tool Disabled','❌ This tool is currently disabled by the administrator.'),
  ('no_access','No Access','🔒 Your current plan does not include this tool.'),
  ('ask_encode_file','Ask Encode File','📤 Send me the .html file you want to protect.'),
  ('ask_encode_password','Ask Encode Password','🔑 Send a password for this file (or send /skip to use an auto-generated one).'),
  ('ask_decode_file','Ask Decode File','📤 Send me the protected .html file to restore.'),
  ('ask_decode_password','Ask Decode Password','🔑 Send the password used to protect this file.'),
  ('ask_url','Ask URL','🔗 Send me the URL (starting with http:// or https://).'),
  ('encode_success','Encode Success','✅ Done! Your protected file is ready.\nAlgorithm: {algo}\nLayers: {layers}'),
  ('decode_success','Decode Success','✅ Restored successfully. Here is your original file.'),
  ('generic_error','Generic Error','❗ Something went wrong: {error}')
ON DUPLICATE KEY UPDATE `title`=VALUES(`title`);

-- Sample premium emoji (replace custom_emoji_id with your own)
INSERT INTO `premium_emojis` (`label`,`placeholder`,`fallback`,`custom_emoji_id`,`is_active`) VALUES
  ('Star','{star}','⭐','5368324170671202286',1)
ON DUPLICATE KEY UPDATE `label`=VALUES(`label`);

SET FOREIGN_KEY_CHECKS = 1;
