-- phpMyAdmin SQL Dump
-- version 5.2.2
-- https://www.phpmyadmin.net/
--
-- Host: localhost:3306
-- Generation Time: Aug 09, 2026 at 11:52 AM
-- Server version: 11.4.12-MariaDB
-- PHP Version: 8.4.23

SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";


/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;

--
-- Database: `karacals_karacalbot`
--

-- --------------------------------------------------------

--
-- Table structure for table `card_payments`
--

CREATE TABLE `card_payments` (
  `id` int(10) UNSIGNED NOT NULL,
  `user_id` bigint(20) NOT NULL,
  `amount` int(11) NOT NULL,
  `status` enum('pending','approved','rejected') NOT NULL DEFAULT 'pending',
  `receipt_file_id` varchar(255) NOT NULL,
  `admin_message_id` int(11) DEFAULT NULL,
  `created_at` datetime NOT NULL DEFAULT current_timestamp(),
  `updated_at` datetime NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci;

-- --------------------------------------------------------

--
-- Table structure for table `conversation_states`
--

CREATE TABLE `conversation_states` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `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()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Table structure for table `payments`
--

CREATE TABLE `payments` (
  `id` int(10) UNSIGNED NOT NULL,
  `user_id` bigint(20) UNSIGNED NOT NULL,
  `invoice_id` varchar(64) NOT NULL,
  `symbol` varchar(10) NOT NULL DEFAULT 'TON',
  `network` varchar(20) NOT NULL DEFAULT 'TON',
  `gateway` varchar(20) NOT NULL DEFAULT 'bahaa',
  `crypto_amount` decimal(18,8) NOT NULL,
  `toman_amount` bigint(20) UNSIGNED NOT NULL,
  `crypto_price_toman` bigint(20) UNSIGNED NOT NULL,
  `status` enum('pending','confirming','completed','expired','cancelled') NOT NULL DEFAULT 'pending',
  `paid_amount` decimal(18,8) DEFAULT NULL,
  `tx_hash` varchar(128) DEFAULT NULL,
  `webhook_delivered_at` datetime DEFAULT NULL,
  `balance_credited` tinyint(1) NOT NULL DEFAULT 0,
  `expires_at` datetime DEFAULT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Table structure for table `pricing_plans`
--

CREATE TABLE `pricing_plans` (
  `id` int(11) UNSIGNED NOT NULL,
  `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()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Dumping data for table `pricing_plans`
--

INSERT INTO `pricing_plans` (`id`, `conn_limit`, `api_price`, `is_active`, `last_synced_at`, `created_at`, `updated_at`) VALUES
(5, 1, 147000, 1, '2026-07-02 15:02:23', '2026-06-01 06:00:50', '2026-07-02 15:02:23'),
(6, 2, 271000, 1, '2026-07-02 15:02:23', '2026-06-01 06:00:50', '2026-07-02 15:02:23'),
(7, 3, 412000, 1, '2026-07-02 15:02:23', '2026-06-01 06:00:50', '2026-07-02 15:02:23'),
(8, 4, 538000, 1, '2026-07-02 15:02:23', '2026-06-01 06:00:50', '2026-07-02 15:02:23');

-- --------------------------------------------------------

--
-- Table structure for table `pricing_settings`
--

CREATE TABLE `pricing_settings` (
  `id` int(11) UNSIGNED NOT NULL,
  `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()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Dumping data for table `pricing_settings`
--

INSERT INTO `pricing_settings` (`id`, `setting_key`, `setting_value`, `description`, `updated_at`) VALUES
(3, 'global_margin', '31', 'درصد سود کلی روی قیمت API', '2026-07-02 16:11:59'),
(4, 'referral_rate_l1_first', '8', 'کمیسیون سطح ۱ - خرید اول', '2026-07-01 18:36:13'),
(5, 'referral_rate_l2_first', '4', 'کمیسیون سطح ۲ - خرید اول', '2026-07-01 18:36:19'),
(6, 'referral_rate_l1_renewal', '5', 'کمیسیون سطح ۱ - تمدید', '2026-07-01 18:36:23'),
(7, 'referral_rate_l2_renewal', '2', 'کمیسیون سطح ۲ - تمدید', '2026-07-01 18:36:28');

-- --------------------------------------------------------

--
-- Table structure for table `referrals`
--

CREATE TABLE `referrals` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `user_id` bigint(20) UNSIGNED NOT NULL COMMENT 'کاربر جدید (ارجاع‌شده)',
  `referrer_id` bigint(20) UNSIGNED NOT NULL COMMENT 'معرف مستقیم - سطح ۱',
  `level2_id` bigint(20) UNSIGNED DEFAULT NULL COMMENT 'معرف غیرمستقیم - سطح ۲',
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Table structure for table `referral_campaigns`
--

CREATE TABLE `referral_campaigns` (
  `id` int(10) UNSIGNED NOT NULL,
  `title` varchar(100) NOT NULL COMMENT 'عنوان کمپین (فقط برای ادمین)',
  `rate_l1` decimal(5,2) NOT NULL COMMENT 'نرخ سطح ۱',
  `rate_l2` decimal(5,2) NOT NULL COMMENT 'نرخ سطح ۲',
  `starts_at` datetime NOT NULL COMMENT 'شروع کمپین',
  `ends_at` datetime NOT NULL COMMENT 'پایان کمپین',
  `is_active` tinyint(1) NOT NULL DEFAULT 1 COMMENT '۱=فعال، ۰=غیرفعال',
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE current_timestamp()
) ;

-- --------------------------------------------------------

--
-- Table structure for table `reseller_profiles`
--

CREATE TABLE `reseller_profiles` (
  `id` int(10) UNSIGNED NOT NULL,
  `user_id` bigint(20) UNSIGNED NOT NULL,
  `tier_id` int(10) UNSIGNED NOT NULL,
  `brand_name` varchar(100) NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Table structure for table `reseller_tiers`
--

CREATE TABLE `reseller_tiers` (
  `id` int(11) UNSIGNED NOT NULL,
  `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()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Dumping data for table `reseller_tiers`
--

INSERT INTO `reseller_tiers` (`id`, `tier_name`, `tier_level`, `discount_percent`, `is_active`, `created_at`, `updated_at`) VALUES
(4, 'استارتر', 1, 0.00, 1, '2026-06-04 13:44:27', '2026-06-11 11:14:18'),
(5, 'پرو', 2, 5.00, 1, '2026-06-04 13:44:57', '2026-06-11 11:15:40'),
(6, 'مستر', 3, 10.00, 1, '2026-06-04 13:45:15', '2026-06-11 11:15:43'),
(9, 'ویژه', 4, 15.00, 1, '2026-06-07 04:53:50', '2026-06-11 11:15:48'),
(10, 'نماینده اصلی', 5, 25.00, 1, '2026-06-07 08:18:13', '2026-06-07 08:20:10'),
(11, 'آشنا', 6, 19.00, 1, '2026-06-21 08:27:16', '2026-06-21 08:27:24');

-- --------------------------------------------------------

--
-- Table structure for table `services`
--

CREATE TABLE `services` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `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','suspended') NOT NULL DEFAULT 'active',
  `warning_count` tinyint(1) UNSIGNED NOT NULL DEFAULT 0,
  `last_warning_at` int(11) UNSIGNED DEFAULT NULL,
  `suspended_until` int(11) UNSIGNED DEFAULT NULL,
  `is_test` tinyint(1) NOT NULL DEFAULT 0,
  `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,
  `notified_3day` tinyint(1) NOT NULL DEFAULT 0,
  `notified_1day` tinyint(1) NOT NULL DEFAULT 0,
  `notified_expired` tinyint(1) NOT NULL DEFAULT 0
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Table structure for table `service_warnings`
--

CREATE TABLE `service_warnings` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `service_id` bigint(20) UNSIGNED NOT NULL,
  `warned_at` int(11) UNSIGNED NOT NULL,
  `expires_at` int(11) UNSIGNED NOT NULL,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COLLATE=latin1_swedish_ci;

-- --------------------------------------------------------

--
-- Table structure for table `tetrapay_orders`
--

CREATE TABLE `tetrapay_orders` (
  `id` int(10) UNSIGNED NOT NULL,
  `user_id` bigint(20) UNSIGNED NOT NULL,
  `hash_id` varchar(64) NOT NULL,
  `authority` varchar(128) DEFAULT NULL,
  `tracking_id` varchar(128) DEFAULT NULL,
  `amount` int(10) UNSIGNED NOT NULL COMMENT 'مبلغ به تومان',
  `status` enum('pending','completed','failed') NOT NULL DEFAULT 'pending',
  `balance_credited` tinyint(1) NOT NULL DEFAULT 0,
  `created_at` timestamp NOT NULL DEFAULT current_timestamp(),
  `updated_at` timestamp NULL DEFAULT NULL ON UPDATE current_timestamp()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Table structure for table `transactions`
--

CREATE TABLE `transactions` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `user_id` bigint(20) UNSIGNED NOT NULL COMMENT 'کاربر مرتبط با تراکنش',
  `type` enum('purchase','renew','refund','change_conn_limit','deposit','transfer_send','transfer_receive','relive','referral_commission') NOT NULL,
  `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()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- --------------------------------------------------------

--
-- Table structure for table `users`
--

CREATE TABLE `users` (
  `id` bigint(20) UNSIGNED NOT NULL,
  `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()
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

--
-- Indexes for dumped tables
--

--
-- Indexes for table `card_payments`
--
ALTER TABLE `card_payments`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_user_id` (`user_id`),
  ADD KEY `idx_status` (`status`);

--
-- Indexes for table `conversation_states`
--
ALTER TABLE `conversation_states`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `user_id` (`user_id`),
  ADD KEY `idx_state` (`state`),
  ADD KEY `idx_expires` (`expires_at`);

--
-- Indexes for table `payments`
--
ALTER TABLE `payments`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `invoice_id` (`invoice_id`),
  ADD UNIQUE KEY `uq_invoice_id` (`invoice_id`),
  ADD KEY `idx_user_id` (`user_id`),
  ADD KEY `idx_status` (`status`),
  ADD KEY `idx_symbol` (`symbol`),
  ADD KEY `idx_created` (`created_at`);

--
-- Indexes for table `pricing_plans`
--
ALTER TABLE `pricing_plans`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `unique_conn_limit` (`conn_limit`),
  ADD KEY `idx_is_active` (`is_active`);

--
-- Indexes for table `pricing_settings`
--
ALTER TABLE `pricing_settings`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `unique_setting_key` (`setting_key`);

--
-- Indexes for table `referrals`
--
ALTER TABLE `referrals`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uq_user_id` (`user_id`),
  ADD KEY `idx_referrer_id` (`referrer_id`),
  ADD KEY `idx_level2_id` (`level2_id`),
  ADD KEY `idx_created_at` (`created_at`);

--
-- Indexes for table `referral_campaigns`
--
ALTER TABLE `referral_campaigns`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_is_active` (`is_active`),
  ADD KEY `idx_starts_at` (`starts_at`),
  ADD KEY `idx_ends_at` (`ends_at`);

--
-- Indexes for table `reseller_profiles`
--
ALTER TABLE `reseller_profiles`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uq_user_id` (`user_id`),
  ADD KEY `idx_tier_id` (`tier_id`);

--
-- Indexes for table `reseller_tiers`
--
ALTER TABLE `reseller_tiers`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `unique_tier_level` (`tier_level`),
  ADD KEY `idx_is_active` (`is_active`);

--
-- Indexes for table `services`
--
ALTER TABLE `services`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `unique_client_id` (`client_id`),
  ADD KEY `idx_user_id` (`user_id`),
  ADD KEY `idx_service_number` (`service_number`),
  ADD KEY `idx_status` (`status`),
  ADD KEY `idx_expires` (`expires_at_api`),
  ADD KEY `idx_services_is_test` (`is_test`),
  ADD KEY `idx_services_suspended_until` (`status`,`suspended_until`);

--
-- Indexes for table `service_warnings`
--
ALTER TABLE `service_warnings`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_service_id` (`service_id`),
  ADD KEY `idx_expires_at` (`expires_at`);

--
-- Indexes for table `tetrapay_orders`
--
ALTER TABLE `tetrapay_orders`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `uq_hash_id` (`hash_id`),
  ADD KEY `idx_user_id` (`user_id`),
  ADD KEY `idx_authority` (`authority`),
  ADD KEY `idx_status` (`status`);

--
-- Indexes for table `transactions`
--
ALTER TABLE `transactions`
  ADD PRIMARY KEY (`id`),
  ADD KEY `idx_user_id` (`user_id`),
  ADD KEY `idx_type` (`type`),
  ADD KEY `idx_service_id` (`service_id`),
  ADD KEY `idx_related_user` (`related_user_id`),
  ADD KEY `idx_created_at` (`created_at`);

--
-- Indexes for table `users`
--
ALTER TABLE `users`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `tg_id` (`tg_id`),
  ADD KEY `idx_tg_id` (`tg_id`),
  ADD KEY `idx_role` (`role`),
  ADD KEY `idx_created` (`created_at`),
  ADD KEY `idx_balance` (`balance`),
  ADD KEY `idx_tier_id` (`tier_id`);

--
-- AUTO_INCREMENT for dumped tables
--

--
-- AUTO_INCREMENT for table `card_payments`
--
ALTER TABLE `card_payments`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `conversation_states`
--
ALTER TABLE `conversation_states`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `payments`
--
ALTER TABLE `payments`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `pricing_plans`
--
ALTER TABLE `pricing_plans`
  MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=9;

--
-- AUTO_INCREMENT for table `pricing_settings`
--
ALTER TABLE `pricing_settings`
  MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=8;

--
-- AUTO_INCREMENT for table `referrals`
--
ALTER TABLE `referrals`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `referral_campaigns`
--
ALTER TABLE `referral_campaigns`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `reseller_profiles`
--
ALTER TABLE `reseller_profiles`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `reseller_tiers`
--
ALTER TABLE `reseller_tiers`
  MODIFY `id` int(11) UNSIGNED NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=12;

--
-- AUTO_INCREMENT for table `services`
--
ALTER TABLE `services`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `service_warnings`
--
ALTER TABLE `service_warnings`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `tetrapay_orders`
--
ALTER TABLE `tetrapay_orders`
  MODIFY `id` int(10) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `transactions`
--
ALTER TABLE `transactions`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- AUTO_INCREMENT for table `users`
--
ALTER TABLE `users`
  MODIFY `id` bigint(20) UNSIGNED NOT NULL AUTO_INCREMENT;

--
-- Constraints for dumped tables
--

--
-- Constraints for table `payments`
--
ALTER TABLE `payments`
  ADD CONSTRAINT `fk_payments_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`tg_id`) ON DELETE CASCADE ON UPDATE CASCADE;

--
-- Constraints for table `referrals`
--
ALTER TABLE `referrals`
  ADD CONSTRAINT `fk_ref_level2` FOREIGN KEY (`level2_id`) REFERENCES `users` (`tg_id`) ON DELETE SET NULL ON UPDATE CASCADE,
  ADD CONSTRAINT `fk_ref_referrer` FOREIGN KEY (`referrer_id`) REFERENCES `users` (`tg_id`) ON DELETE CASCADE ON UPDATE CASCADE,
  ADD CONSTRAINT `fk_ref_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`tg_id`) ON DELETE CASCADE ON UPDATE CASCADE;

--
-- Constraints for table `reseller_profiles`
--
ALTER TABLE `reseller_profiles`
  ADD CONSTRAINT `fk_rp_tier` FOREIGN KEY (`tier_id`) REFERENCES `reseller_tiers` (`id`) ON UPDATE CASCADE,
  ADD CONSTRAINT `fk_rp_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

--
-- Constraints for table `service_warnings`
--
ALTER TABLE `service_warnings`
  ADD CONSTRAINT `fk_sw_service` FOREIGN KEY (`service_id`) REFERENCES `services` (`id`) ON DELETE CASCADE ON UPDATE CASCADE;

--
-- Constraints for table `tetrapay_orders`
--
ALTER TABLE `tetrapay_orders`
  ADD CONSTRAINT `fk_tetrapay_user` FOREIGN KEY (`user_id`) REFERENCES `users` (`tg_id`) ON DELETE CASCADE ON UPDATE CASCADE;

--
-- Constraints for table `users`
--
ALTER TABLE `users`
  ADD CONSTRAINT `fk_users_tier` FOREIGN KEY (`tier_id`) REFERENCES `reseller_tiers` (`id`) ON DELETE SET NULL ON UPDATE CASCADE;
COMMIT;

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
