-- Initial Schema for P2P Crypto Exchange Bot

CREATE TABLE IF NOT EXISTS `users` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `telegram_id` BIGINT UNIQUE NOT NULL,
  `nickname` VARCHAR(18) UNIQUE NOT NULL,
  `username` VARCHAR(255),
  `language` VARCHAR(10) DEFAULT 'en',
  `phone_number` VARCHAR(20),
  `is_verified` BOOLEAN DEFAULT FALSE,
  `rating` FLOAT DEFAULT 5.0,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `role` ENUM('user', 'admin') DEFAULT 'user'
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `wallets` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `user_id` INT NOT NULL,
  `currency` VARCHAR(10) NOT NULL,
  `available` DECIMAL(20, 8) DEFAULT 0.0,
  `reserved` DECIMAL(20, 8) DEFAULT 0.0,
  UNIQUE KEY `user_currency` (`user_id`, `currency`),
  FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `ads` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `user_id` INT NOT NULL,
  `ad_type` ENUM('SELL', 'BUY') NOT NULL,
  `currency` VARCHAR(10) NOT NULL DEFAULT 'TON',
  `payment_currency` VARCHAR(10) NOT NULL DEFAULT 'RUB',
  `price_type` ENUM('FIXED', 'FORMULA') NOT NULL,
  `fixed_price` DECIMAL(20, 2) DEFAULT NULL,
  `formula_add` DECIMAL(20, 2) DEFAULT NULL,
  `min_amount` DECIMAL(20, 8) NOT NULL,
  `max_amount` DECIMAL(20, 8) NOT NULL,
  `available_amount` DECIMAL(20, 8) NOT NULL,
  `bank_name` VARCHAR(255) NOT NULL,
  `bank_details` TEXT,
  `comment` TEXT,
  `start_time` TIME,
  `end_time` TIME,
  `status` ENUM('ACTIVE', 'PAUSED', 'CLOSED') DEFAULT 'ACTIVE',
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (`user_id`) REFERENCES `users`(`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `orders` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `ad_id` INT NOT NULL,
  `buyer_id` INT NOT NULL,
  `seller_id` INT NOT NULL,
  `amount_crypto` DECIMAL(20, 8) NOT NULL,
  `amount_fiat` DECIMAL(20, 2) NOT NULL,
  `escrow_fee` DECIMAL(20, 8) DEFAULT 0.0,
  `status` ENUM('PENDING_CONFIRMATION', 'AWAITING_PAYMENT', 'AWAITING_SELLER_CONFIRMATION', 'COMPLETED', 'CANCELED', 'DISPUTED') NOT NULL,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  `updated_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  FOREIGN KEY (`ad_id`) REFERENCES `ads`(`id`),
  FOREIGN KEY (`buyer_id`) REFERENCES `users`(`id`),
  FOREIGN KEY (`seller_id`) REFERENCES `users`(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `transactions` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `user_id` INT NOT NULL,
  `order_id` INT,
  `type` ENUM('DEPOSIT', 'WITHDRAW', 'HOLD', 'RELEASE', 'TRANSFER', 'FEE', 'DISPUTE_REFUND') NOT NULL,
  `currency` VARCHAR(10) NOT NULL,
  `amount` DECIMAL(20, 8) NOT NULL,
  `description` VARCHAR(255),
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (`user_id`) REFERENCES `users`(`id`),
  FOREIGN KEY (`order_id`) REFERENCES `orders`(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `disputes` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `order_id` INT NOT NULL,
  `created_by_id` INT NOT NULL,
  `reason` TEXT,
  `status` ENUM('OPEN', 'RESOLVED_BUYER', 'RESOLVED_SELLER', 'RESOLVED_SPLIT') DEFAULT 'OPEN',
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (`order_id`) REFERENCES `orders`(`id`),
  FOREIGN KEY (`created_by_id`) REFERENCES `users`(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

CREATE TABLE IF NOT EXISTS `messages` (
  `id` INT AUTO_INCREMENT PRIMARY KEY,
  `order_id` INT NOT NULL,
  `sender_id` INT NOT NULL,
  `receiver_id` INT NOT NULL,
  `message` TEXT NOT NULL,
  `is_read` BOOLEAN DEFAULT FALSE,
  `created_at` TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (`order_id`) REFERENCES `orders`(`id`),
  FOREIGN KEY (`sender_id`) REFERENCES `users`(`id`),
  FOREIGN KEY (`receiver_id`) REFERENCES `users`(`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
