CREATE TABLE `conversation_states` (
 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
 `user_id` bigint(20) unsigned NOT NULL,
 `state` varchar(100) NOT NULL,
 `data` text DEFAULT NULL,
 `expires_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
 `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
 `updated_at` timestamp NULL DEFAULT NULL ON UPDATE current_timestamp(),
 PRIMARY KEY (`id`),
 UNIQUE KEY `user_id` (`user_id`),
 KEY `idx_state` (`state`),
 KEY `idx_expires` (`expires_at`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

CREATE TABLE `pricing_plans` (
 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
 `conn_limit` int(11) NOT NULL COMMENT 'تعداد کاربر مجاز',
 `api_price` bigint(20) NOT NULL COMMENT 'قیمت پایه API به تومان',
 `is_active` tinyint(1) NOT NULL DEFAULT 1,
 `last_synced_at` timestamp NULL DEFAULT NULL COMMENT 'آخرین sync با API',
 `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
 `updated_at` timestamp NULL DEFAULT NULL ON UPDATE current_timestamp(),
 PRIMARY KEY (`id`),
 UNIQUE KEY `unique_conn_limit` (`conn_limit`),
 KEY `idx_is_active` (`is_active`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

CREATE TABLE `pricing_settings` (
 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
 `setting_key` varchar(100) NOT NULL,
 `setting_value` text NOT NULL,
 `description` varchar(255) DEFAULT NULL,
 `updated_at` timestamp NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
 PRIMARY KEY (`id`),
 UNIQUE KEY `unique_setting_key` (`setting_key`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

CREATE TABLE `reseller_tiers` (
 `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
 `tier_name` varchar(100) NOT NULL COMMENT 'نام سطح',
 `tier_level` int(11) NOT NULL COMMENT 'سطح (1، 2، 3، ...)',
 `discount_percent` decimal(5,2) NOT NULL COMMENT 'درصد تخفیف',
 `is_active` tinyint(1) NOT NULL DEFAULT 1,
 `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
 `updated_at` timestamp NULL DEFAULT NULL ON UPDATE current_timestamp(),
 PRIMARY KEY (`id`),
 UNIQUE KEY `unique_tier_level` (`tier_level`),
 KEY `idx_is_active` (`is_active`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

CREATE TABLE `services` (
 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
 `user_id` bigint(20) unsigned NOT NULL COMMENT 'مالک سرویس',
 `client_id` varchar(100) NOT NULL COMMENT 'شناسه کلاینت از API (مثل 1419455949:3)',
 `service_number` int(11) NOT NULL COMMENT 'شماره سرویس',
 `name` varchar(255) NOT NULL COMMENT 'نام سرویس',
 `note` text DEFAULT NULL COMMENT 'یادداشت سرویس',
 `conn_limit` int(11) NOT NULL COMMENT 'تعداد دستگاه مجاز',
 `created_at_api` int(11) NOT NULL COMMENT 'زمان ساخت در API (timestamp)',
 `expires_at_api` int(11) NOT NULL COMMENT 'زمان انقضا در API (timestamp)',
 `price_paid` bigint(20) NOT NULL COMMENT 'قیمت پرداختی کاربر (تومان)',
 `cost_price` bigint(20) NOT NULL COMMENT 'قیمت API (تومان)',
 `direct_link` text DEFAULT NULL COMMENT 'لینک مستقیم اتصال',
 `subscription_link` text DEFAULT NULL COMMENT 'لینک اشتراک',
 `qrcode_link` text DEFAULT NULL COMMENT 'لینک QR Code',
 `status` enum('active','expired','expiring_soon','refunded') NOT NULL DEFAULT 'active',
 `is_synced` tinyint(1) NOT NULL DEFAULT 1 COMMENT 'آیا با API همگام است',
 `last_synced_at` timestamp NULL DEFAULT NULL COMMENT 'آخرین زمان همگام‌سازی',
 `refunded_at` timestamp NULL DEFAULT NULL COMMENT 'زمان مرجوع شدن',
 `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
 `updated_at` timestamp NULL DEFAULT NULL ON UPDATE current_timestamp(),
 `upload_bytes` bigint(20) DEFAULT 0,
 `download_bytes` bigint(20) DEFAULT 0,
 PRIMARY KEY (`id`),
 UNIQUE KEY `unique_client_id` (`client_id`),
 KEY `idx_user_id` (`user_id`),
 KEY `idx_service_number` (`service_number`),
 KEY `idx_status` (`status`),
 KEY `idx_expires` (`expires_at_api`)
) ENGINE=InnoDB AUTO_INCREMENT=101 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

CREATE TABLE `transactions` (
 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
 `user_id` bigint(20) unsigned NOT NULL COMMENT 'کاربر مرتبط با تراکنش',
 `type` enum('purchase','renew','refund','change_conn_limit','deposit','transfer_send','transfer_receive','relive') NOT NULL COMMENT 'نوع تراکنش',
 `amount` bigint(20) NOT NULL COMMENT 'مبلغ (مثبت یا منفی)',
 `balance_before` bigint(20) NOT NULL COMMENT 'موجودی قبل از تراکنش',
 `balance_after` bigint(20) NOT NULL COMMENT 'موجودی بعد از تراکنش',
 `service_id` bigint(20) unsigned DEFAULT NULL COMMENT 'سرویس مرتبط (nullable)',
 `related_user_id` bigint(20) unsigned DEFAULT NULL COMMENT 'کاربر مقصد در انتقال موجودی',
 `metadata` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin DEFAULT NULL COMMENT 'اطلاعات تفصیلی به صورت JSON' CHECK (json_valid(`metadata`)),
 `description` varchar(500) DEFAULT NULL COMMENT 'توضیحات',
 `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
 PRIMARY KEY (`id`),
 KEY `idx_user_id` (`user_id`),
 KEY `idx_type` (`type`),
 KEY `idx_service_id` (`service_id`),
 KEY `idx_related_user` (`related_user_id`),
 KEY `idx_created_at` (`created_at`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci

CREATE TABLE `users` (
 `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
 `tg_id` bigint(20) unsigned NOT NULL,
 `role` enum('user','reseller','admin') NOT NULL DEFAULT 'user',
 `balance` bigint(20) NOT NULL DEFAULT 0 COMMENT 'موجودی به تومان',
 `tier_id` int(11) unsigned DEFAULT NULL COMMENT 'سطح نمایندگی',
 `is_banned` tinyint(1) NOT NULL DEFAULT 0,
 `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
 `updated_at` timestamp NULL DEFAULT NULL ON UPDATE current_timestamp(),
 PRIMARY KEY (`id`),
 UNIQUE KEY `tg_id` (`tg_id`),
 KEY `idx_tg_id` (`tg_id`),
 KEY `idx_role` (`role`),
 KEY `idx_created` (`created_at`),
 KEY `idx_balance` (`balance`),
 KEY `idx_tier_id` (`tier_id`),
 CONSTRAINT `fk_users_tier` FOREIGN KEY (`tier_id`) REFERENCES `reseller_tiers` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci
