CREATE TABLE IF NOT EXISTS settings(key TEXT PRIMARY KEY, value TEXT NOT NULL DEFAULT '');
INSERT OR IGNORE INTO settings(key,value) VALUES
('headline_a','Air Biru Susu Rare – Sekali Pandang Terhenti, Sekali Minum Terus Repeat!'),
('headline_b','Vanilla Blue: Sekali Bancuh, Beratur Panjang'),
('founder_story','Mukmin Rich bantu peniaga kecil capai rezeki halal—mudah, sedap & konsisten.'),
('ORDER_BUMP_PRICE','5.00'),
('UPSELL_PRICE','10.00'),
('DOWNSELL_PRICE','5.00'),
('EXIT_INTENT_COUPON_CODE','JANGANPERGI10'),
('EXIT_INTENT_PERCENT','10'),
('ADMIN_PHONE','+60123456789'),
('RECAPTCHA_SITE_KEY',''),
('RECAPTCHA_SECRET_KEY',''),
('TOYYIBPAY_API_KEY',''),
('TOYYIBPAY_CATEGORY_CODE',''),
('TOYYIBPAY_RETURN_URL',''),
('TOYYIBPAY_CALLBACK_SECRET',''),
('MAILCHIMP_API_KEY',''),
('MAILCHIMP_AUDIENCE_ID',''),
('WHATSAPP_API_URL',''),
('WHATSAPP_API_TOKEN','');

CREATE TABLE IF NOT EXISTS packages(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, price REAL, features TEXT, best_seller INTEGER DEFAULT 0, active INTEGER DEFAULT 1, stock INTEGER DEFAULT 9999);
INSERT OR IGNORE INTO packages(id,name,price,features,best_seller,active,stock) VALUES
(1,'Pakej Percubaan',12.60,'1 pek 1kg Vanilla Blue|Ebook Resepi Viral',0,1,9999),
(2,'Pakej Berbaloi',35.00,'3 pek 1kg Vanilla Blue|Ebook Resepi Viral',1,1,9999),
(3,'Pakej Borong',55.00,'5 pek 1kg Vanilla Blue|Ebook Resepi Viral|Pos Percuma',0,1,9999);

CREATE TABLE IF NOT EXISTS testimonials(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, text TEXT, active INTEGER DEFAULT 1);
INSERT OR IGNORE INTO testimonials(id,name,text,active) VALUES
(1,'Ibu Fara','Anak-anak asyik minta lagi bila bancuh Vanilla Blue.',1),
(2,'Abg Man Gerai','Modal kecil tapi orang beratur. Alhamdulillah.',1),
(3,'Cik Din Café','Rasa premium, senang standardize resepi.',1);

CREATE TABLE IF NOT EXISTS doa(id INTEGER PRIMARY KEY AUTOINCREMENT, text TEXT, active INTEGER DEFAULT 1);
INSERT OR IGNORE INTO doa(text,active) VALUES ('Ya Allah, berkati rezeki kami.',1),('Mudahkan urusan kami, Ya Rabb.',1);

CREATE TABLE IF NOT EXISTS coupons(code TEXT PRIMARY KEY, amount_off REAL DEFAULT 0, percent_off REAL DEFAULT 0, active INTEGER DEFAULT 1, usage_limit INTEGER, used INTEGER DEFAULT 0);
INSERT OR IGNORE INTO coupons(code,percent_off,active,usage_limit,used) VALUES('JANGANPERGI10',10,1,1000,0);

CREATE TABLE IF NOT EXISTS orders(
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  created_at TEXT NOT NULL DEFAULT (datetime('now')),
  name TEXT, email TEXT, phone TEXT, address TEXT,
  package_id INTEGER, package_name TEXT,
  payment_method TEXT, amount REAL, order_bump INTEGER DEFAULT 0, order_bump_price REAL DEFAULT 0,
  status TEXT DEFAULT 'Pending', tracking_no TEXT,
  bill_code TEXT, coupon_code TEXT, coupon_discount REAL DEFAULT 0,
  utm_source TEXT, utm_medium TEXT, utm_campaign TEXT, utm_content TEXT, utm_term TEXT, affiliate TEXT, ref TEXT,
  recovery_sent INTEGER DEFAULT 0,
  FOREIGN KEY(package_id) REFERENCES packages(id)
);

CREATE TABLE IF NOT EXISTS admins(id INTEGER PRIMARY KEY AUTOINCREMENT, username TEXT UNIQUE, password_hash TEXT);
INSERT OR IGNORE INTO admins(id,username,password_hash) VALUES (1,'admin','$2y$10$3L8kE1y1wqG5x2n8G0bU7OeQf4mA6oVf4w0oT1T8Uq6sG3sFzqJ1S'); -- admin123

CREATE TABLE IF NOT EXISTS visits(
  id INTEGER PRIMARY KEY AUTOINCREMENT,
  created_at TEXT NOT NULL DEFAULT (datetime('now')),
  ip TEXT, ua TEXT, ab_variant TEXT,
  utm_source TEXT, utm_medium TEXT, utm_campaign TEXT, utm_content TEXT, utm_term TEXT, affiliate TEXT, ref TEXT
);

CREATE TABLE IF NOT EXISTS faq(id INTEGER PRIMARY KEY AUTOINCREMENT, q TEXT, a TEXT, active INTEGER DEFAULT 1);
INSERT OR IGNORE INTO faq(q,a,active) VALUES
('Halal ke produk ni?','Produk mengutamakan bahan yang selamat digunakan. Rujuk label & sijil terkini di halaman produk.',1),
('Cara bancuh?','1 pek 1kg → 8-10 liter; tambah ais ikut selera. Untuk air balang, ikut nisbah standard gerai.',1);

CREATE TABLE IF NOT EXISTS ai_logs(id INTEGER PRIMARY KEY AUTOINCREMENT, created_at TEXT NOT NULL DEFAULT (datetime('now')), kind TEXT, input TEXT, output TEXT, meta TEXT);