CREATE DATABASE IF NOT EXISTS gokios_h2h CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE gokios_h2h;

CREATE TABLE users (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 member_code VARCHAR(32) NOT NULL UNIQUE,
 name VARCHAR(120) NOT NULL,
 email VARCHAR(190) NOT NULL UNIQUE,
 phone VARCHAR(32) NULL,
 password_hash VARCHAR(255) NOT NULL,
 api_key VARCHAR(80) NOT NULL UNIQUE,
 api_secret VARCHAR(128) NOT NULL,
 ip_whitelist TEXT NULL,
 callback_url VARCHAR(500) NULL,
 balance DECIMAL(15,2) NOT NULL DEFAULT 0.00,
 status ENUM('active','suspended') NOT NULL DEFAULT 'active',
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 INDEX idx_users_status(status)
) ENGINE=InnoDB;

CREATE TABLE products (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 buyer_sku_code VARCHAR(80) NOT NULL UNIQUE,
 product_name VARCHAR(190) NOT NULL,
 category VARCHAR(80) NULL,
 brand VARCHAR(80) NULL,
 type VARCHAR(80) NULL,
 cost_price DECIMAL(15,2) NOT NULL DEFAULT 0,
 member_price DECIMAL(15,2) NOT NULL DEFAULT 0,
 is_active TINYINT(1) NOT NULL DEFAULT 1,
 seller_product_status TINYINT(1) NOT NULL DEFAULT 1,
 unlimited_stock TINYINT(1) NOT NULL DEFAULT 0,
 multi TINYINT(1) NOT NULL DEFAULT 0,
 `desc` TEXT NULL,
 synced_at DATETIME NULL,
 INDEX idx_products_filter(category,brand,is_active),
 INDEX idx_products_active(is_active)
) ENGINE=InnoDB;

CREATE TABLE transactions (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 trx_id VARCHAR(120) NULL,
 ref_id VARCHAR(100) NOT NULL,
 member_id BIGINT UNSIGNED NOT NULL,
 buyer_sku_code VARCHAR(80) NOT NULL,
 customer_no VARCHAR(100) NOT NULL,
 price DECIMAL(15,2) NOT NULL,
 cost_price DECIMAL(15,2) NOT NULL,
 profit DECIMAL(15,2) NOT NULL DEFAULT 0,
 status ENUM('Pending','Success','Failed') NOT NULL DEFAULT 'Pending',
 sn VARCHAR(255) NULL,
 message TEXT NULL,
 raw_request LONGTEXT NULL,
 raw_response LONGTEXT NULL,
 callback_sent TINYINT(1) NOT NULL DEFAULT 0,
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
 UNIQUE KEY uq_member_ref(member_id,ref_id),
 INDEX idx_trx_status(status),
 INDEX idx_trx_created(created_at),
 CONSTRAINT fk_trx_member FOREIGN KEY(member_id) REFERENCES users(id) ON DELETE RESTRICT
) ENGINE=InnoDB;

CREATE TABLE balance_mutations (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 member_id BIGINT UNSIGNED NOT NULL,
 type ENUM('credit','debit') NOT NULL,
 amount DECIMAL(15,2) NOT NULL,
 balance_before DECIMAL(15,2) NOT NULL,
 balance_after DECIMAL(15,2) NOT NULL,
 reference_type ENUM('topup','transaction','adjustment','refund') NOT NULL,
 reference_id BIGINT UNSIGNED NOT NULL,
 description VARCHAR(255) NULL,
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 INDEX idx_mut_member_created(member_id,created_at),
 INDEX idx_mut_reference(reference_type,reference_id),
 CONSTRAINT fk_mut_member FOREIGN KEY(member_id) REFERENCES users(id) ON DELETE RESTRICT
) ENGINE=InnoDB;

CREATE TABLE topup_requests (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 topup_code VARCHAR(60) NOT NULL UNIQUE,
 member_id BIGINT UNSIGNED NOT NULL,
 bank_or_method VARCHAR(80) NOT NULL,
 amount DECIMAL(15,2) NOT NULL,
 unique_code DECIMAL(15,2) NOT NULL DEFAULT 0,
 total_transfer DECIMAL(15,2) NOT NULL,
 proof_image_path VARCHAR(500) NULL,
 note TEXT NULL,
 status ENUM('pending','approved','rejected') NOT NULL DEFAULT 'pending',
 telegram_chat_id BIGINT NULL,
 telegram_message_id BIGINT NULL,
 reviewed_by BIGINT NULL,
 reviewed_at DATETIME NULL,
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 INDEX idx_topup_status(status,created_at),
 INDEX idx_topup_member(member_id,created_at),
 CONSTRAINT fk_topup_member FOREIGN KEY(member_id) REFERENCES users(id) ON DELETE RESTRICT
) ENGINE=InnoDB;

CREATE TABLE api_logs (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 member_id BIGINT UNSIGNED NULL,
 endpoint VARCHAR(255) NOT NULL,
 method VARCHAR(10) NOT NULL,
 ip_address VARCHAR(45) NOT NULL,
 request_payload LONGTEXT NULL,
 response_payload LONGTEXT NULL,
 http_code SMALLINT UNSIGNED NOT NULL,
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 INDEX idx_api_logs_member_created(member_id,created_at),
 INDEX idx_api_logs_created(created_at),
 CONSTRAINT fk_api_log_member FOREIGN KEY(member_id) REFERENCES users(id) ON DELETE SET NULL
) ENGINE=InnoDB;

CREATE TABLE csrf_tokens (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 session_id VARCHAR(128) NOT NULL,
 token_hash CHAR(64) NOT NULL,
 expires_at DATETIME NOT NULL,
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 UNIQUE KEY uq_csrf_token(token_hash),
 INDEX idx_csrf_session(session_id),
 INDEX idx_csrf_exp(expires_at)
) ENGINE=InnoDB;

-- Optional admin table for dashboard authorization.
CREATE TABLE admins (
 id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 user_id BIGINT UNSIGNED NOT NULL UNIQUE,
 role ENUM('superadmin','admin') NOT NULL DEFAULT 'admin',
 created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
 CONSTRAINT fk_admin_user FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
) ENGINE=InnoDB;
