CREATE DATABASE IF NOT EXISTS suarahati_db DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

USE suarahati_db;

-- Warranty activations table
CREATE TABLE IF NOT EXISTS garansi (
    id INT AUTO_INCREMENT PRIMARY KEY,
    serial_number VARCHAR(50) NOT NULL UNIQUE,
    nama_pembeli VARCHAR(100) NOT NULL,
    no_hp VARCHAR(20) NOT NULL,
    alamat TEXT,
    tipe_produk VARCHAR(100),
    nama_produk VARCHAR(100) NOT NULL,
    durasi_garansi VARCHAR(50) NOT NULL,
    tgl_beli DATE NOT NULL,
    tgl_habis DATE NOT NULL,
    foto_bukti VARCHAR(255) DEFAULT NULL,
    cabang_toko VARCHAR(100) NOT NULL DEFAULT 'Cabang Utama',
    status_aktivasi ENUM('Teraktivasi', 'Kadaluarsa', 'Dihapus') NOT NULL DEFAULT 'Teraktivasi',
    created_by INT DEFAULT NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (created_by) REFERENCES admin(id) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;

-- Index for search
CREATE INDEX idx_serial_number ON garansi(serial_number);
CREATE INDEX idx_nama_pembeli ON garansi(nama_pembeli);
CREATE INDEX idx_status ON garansi(status_aktivasi);
