SET NAMES utf8mb4;
CREATE TABLE admins (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 username VARCHAR(80) NOT NULL UNIQUE,
 password_hash VARCHAR(255) NOT NULL,
 created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;

CREATE TABLE customers (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 name VARCHAR(120) NOT NULL,
 email VARCHAR(190) NOT NULL UNIQUE,
 password_hash VARCHAR(255) NOT NULL,
 status ENUM('pending','active','suspended') NOT NULL DEFAULT 'pending',
 email_verified TINYINT(1) NOT NULL DEFAULT 0,
 referral_code VARCHAR(32) NOT NULL UNIQUE,
 referred_by BIGINT UNSIGNED NULL,
 created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
 CONSTRAINT fk_customer_referrer FOREIGN KEY (referred_by) REFERENCES customers(id) ON DELETE SET NULL
) ENGINE=InnoDB;

CREATE TABLE categories (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 parent_id BIGINT UNSIGNED NULL,
 name VARCHAR(120) NOT NULL,
 slug VARCHAR(140) NOT NULL UNIQUE,
 sort_order INT NOT NULL DEFAULT 0,
 is_active TINYINT(1) NOT NULL DEFAULT 1,
 FOREIGN KEY (parent_id) REFERENCES categories(id) ON DELETE SET NULL
) ENGINE=InnoDB;

CREATE TABLE products (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 category_id BIGINT UNSIGNED NULL,
 name VARCHAR(180) NOT NULL,
 description TEXT NULL,
 image_url VARCHAR(500) NULL,
 price DECIMAL(12,2) NOT NULL,
 is_active TINYINT(1) NOT NULL DEFAULT 1,
 sort_order INT NOT NULL DEFAULT 0,
 created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
 FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE SET NULL,
 CHECK (price >= 0)
) ENGINE=InnoDB;

CREATE TABLE digital_codes (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 product_id BIGINT UNSIGNED NOT NULL,
 code_ciphertext TEXT NOT NULL,
 status ENUM('available','assigned','disabled') NOT NULL DEFAULT 'available',
 assigned_order_id BIGINT UNSIGNED NULL,
 created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
 INDEX idx_stock (product_id,status),
 UNIQUE KEY uq_code_assignment (assigned_order_id),
 FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE CASCADE
) ENGINE=InnoDB;

CREATE TABLE orders (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 customer_id BIGINT UNSIGNED NOT NULL,
 product_id BIGINT UNSIGNED NOT NULL,
 amount DECIMAL(12,2) NOT NULL,
 payment_method ENUM('wallet','manual') NOT NULL,
 status ENUM('pending_payment','paid','delivered','cancel_requested','cancelled','refunded') NOT NULL DEFAULT 'pending_payment',
 created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
 paid_at DATETIME NULL,
 delivered_at DATETIME NULL,
 FOREIGN KEY (customer_id) REFERENCES customers(id),
 FOREIGN KEY (product_id) REFERENCES products(id)
) ENGINE=InnoDB;

ALTER TABLE digital_codes ADD CONSTRAINT fk_code_order FOREIGN KEY (assigned_order_id) REFERENCES orders(id) ON DELETE SET NULL;

CREATE TABLE wallet_accounts (
 customer_id BIGINT UNSIGNED PRIMARY KEY,
 balance DECIMAL(12,2) NOT NULL DEFAULT 0,
 updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 FOREIGN KEY (customer_id) REFERENCES customers(id) ON DELETE CASCADE,
 CHECK (balance >= 0)
) ENGINE=InnoDB;

CREATE TABLE wallet_ledger (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 customer_id BIGINT UNSIGNED NOT NULL,
 amount DECIMAL(12,2) NOT NULL,
 entry_type ENUM('deposit','purchase','referral_reward','admin_adjustment','refund') NOT NULL,
 reference_type VARCHAR(40) NULL,
 reference_id BIGINT UNSIGNED NULL,
 note VARCHAR(500) NOT NULL,
 created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
 INDEX idx_ledger_customer (customer_id,created_at),
 FOREIGN KEY (customer_id) REFERENCES customers(id)
) ENGINE=InnoDB;

CREATE TABLE deposits (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 customer_id BIGINT UNSIGNED NOT NULL,
 method ENUM('bkash','nagad') NOT NULL,
 amount DECIMAL(12,2) NOT NULL,
 transaction_id VARCHAR(120) NOT NULL,
 status ENUM('pending','approved','rejected') NOT NULL DEFAULT 'pending',
 reviewed_by BIGINT UNSIGNED NULL,
 reviewed_at DATETIME NULL,
 created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
 UNIQUE KEY uq_payment_transaction (method,transaction_id),
 FOREIGN KEY (customer_id) REFERENCES customers(id),
 FOREIGN KEY (reviewed_by) REFERENCES admins(id)
) ENGINE=InnoDB;

CREATE TABLE referral_rewards (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 deposit_id BIGINT UNSIGNED NOT NULL UNIQUE,
 referrer_customer_id BIGINT UNSIGNED NOT NULL,
 referred_customer_id BIGINT UNSIGNED NOT NULL,
 reward_amount DECIMAL(12,2) NOT NULL,
 created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
 FOREIGN KEY (deposit_id) REFERENCES deposits(id),
 FOREIGN KEY (referrer_customer_id) REFERENCES customers(id),
 FOREIGN KEY (referred_customer_id) REFERENCES customers(id)
) ENGINE=InnoDB;

CREATE TABLE admin_audit_logs (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 admin_id BIGINT UNSIGNED NOT NULL,
 action VARCHAR(120) NOT NULL,
 details TEXT NULL,
 ip_address VARCHAR(45) NULL,
 created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
 INDEX idx_audit_created (created_at),
 FOREIGN KEY (admin_id) REFERENCES admins(id)
) ENGINE=InnoDB;

CREATE TABLE site_settings (
 setting_key VARCHAR(100) PRIMARY KEY,
 setting_value TEXT NOT NULL,
 updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP
) ENGINE=InnoDB;

INSERT INTO site_settings(setting_key,setting_value) VALUES
('shop_name','REXO SHOP'),
('currency','BDT'),
('nagad_number','01972982173'),
('bkash_number',''),
('support_telegram','@Rexo_akash'),
('welcome_bn','REXO SHOP-এ আপনাকে স্বাগতম — সেরা দামে সেরা গেমিং ও ডিজিটাল সার্ভিস।'),
('welcome_en','Welcome to REXO SHOP — the best gaming and digital services at great prices.'),
('referral_enabled','1');
