CREATE TABLE IF NOT EXISTS sesi (
    id INT AUTO_INCREMENT PRIMARY KEY,
    kode_reseller VARCHAR(20) NOT NULL,
    nama_reseller VARCHAR(100),
    token VARCHAR(64) NOT NULL UNIQUE,
    pin VARCHAR(20),
    password VARCHAR(50),
    foto TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    expired_at TIMESTAMP DEFAULT (CURRENT_TIMESTAMP + INTERVAL 30 DAY)
);

CREATE TABLE IF NOT EXISTS riwayat (
    id INT AUTO_INCREMENT PRIMARY KEY,
    kode_reseller VARCHAR(20) NOT NULL,
    trxid VARCHAR(50) NOT NULL,
    produk VARCHAR(100),
    msisdn VARCHAR(30),
    harga DECIMAL(12,0) DEFAULT 0,
    status ENUM('sukses','gagal','pending') DEFAULT 'pending',
    sn TEXT,
    msg TEXT,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS topup_request (
    id INT AUTO_INCREMENT PRIMARY KEY,
    kode_reseller VARCHAR(20) NOT NULL,
    nominal DECIMAL(12,0) NOT NULL,
    bank_id INT DEFAULT 0,
    nama_bank VARCHAR(50),
    no_rekening VARCHAR(30),
    nama_pemilik VARCHAR(100),
    pengirim VARCHAR(100),
    bukti_base64 MEDIUMTEXT,
    status ENUM('pending','diproses','sukses','ditolak') DEFAULT 'pending',
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS bank (
    id INT AUTO_INCREMENT PRIMARY KEY,
    nama_bank VARCHAR(50) NOT NULL,
    no_rekening VARCHAR(30) NOT NULL,
    nama_pemilik VARCHAR(100) NOT NULL,
    logo VARCHAR(10),
    aktif TINYINT(1) DEFAULT 1
);

CREATE TABLE IF NOT EXISTS notifikasi (
    id INT AUTO_INCREMENT PRIMARY KEY,
    kode_reseller VARCHAR(20),
    judul VARCHAR(200),
    isi TEXT,
    dibaca TINYINT(1) DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS chat (
    id INT AUTO_INCREMENT PRIMARY KEY,
    kode_reseller VARCHAR(20) NOT NULL,
    pengirim ENUM('reseller','admin') DEFAULT 'reseller',
    pesan TEXT,
    tipe VARCHAR(20) DEFAULT 'teks',
    dibaca TINYINT(1) DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

-- Data bank default
INSERT IGNORE INTO bank (id, nama_bank, no_rekening, nama_pemilik, logo) VALUES
(1, 'BRI', '1234567890', 'Nama Pemilik', 'BRI'),
(2, 'BCA', '0987654321', 'Nama Pemilik', 'BCA');
