-- WFLIX MARKET MARKETPLACE
-- Ejecuta este archivo dentro de tu base de datos existente.
-- No borra las tablas actuales. Crea/extiende lo necesario.

CREATE TABLE IF NOT EXISTS admins(
 id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 name VARCHAR(120) NOT NULL,
 email VARCHAR(190) NOT NULL UNIQUE,
 password VARCHAR(255) NOT NULL,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS categories(
 id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 name VARCHAR(120) NOT NULL,
 icon VARCHAR(20) DEFAULT '✦',
 sort_order INT DEFAULT 0,
 active TINYINT(1) DEFAULT 1
);

CREATE TABLE IF NOT EXISTS products(
 id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 category_id INT UNSIGNED NULL,
 name VARCHAR(180) NOT NULL,
 description TEXT,
 price DECIMAL(12,2) NOT NULL DEFAULT 0,
 old_price DECIMAL(12,2) NOT NULL DEFAULT 0,
 image VARCHAR(255) DEFAULT '',
 featured TINYINT(1) DEFAULT 0,
 sale TINYINT(1) DEFAULT 0,
 is_new TINYINT(1) DEFAULT 0,
 active TINYINT(1) DEFAULT 1,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 INDEX(category_id)
);

CREATE TABLE IF NOT EXISTS settings(
 id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 `key` VARCHAR(100) NOT NULL UNIQUE,
 value TEXT
);

CREATE TABLE IF NOT EXISTS promotions(
 id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 title VARCHAR(180) NOT NULL,
 subtitle VARCHAR(255) DEFAULT '',
 discount VARCHAR(60) DEFAULT '',
 image VARCHAR(255) DEFAULT '',
 link VARCHAR(255) DEFAULT '',
 active TINYINT(1) DEFAULT 1,
 sort_order INT DEFAULT 0,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS banners(
 id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 title VARCHAR(180) NOT NULL,
 subtitle VARCHAR(255) DEFAULT '',
 image VARCHAR(255) DEFAULT '',
 button_text VARCHAR(80) DEFAULT 'Ver productos',
 button_url VARCHAR(255) DEFAULT 'tienda.php',
 active TINYINT(1) DEFAULT 1,
 sort_order INT DEFAULT 0,
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE TABLE IF NOT EXISTS customers(
 id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 name VARCHAR(150) NOT NULL,
 phone VARCHAR(40) DEFAULT '',
 email VARCHAR(190) DEFAULT '',
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 INDEX(phone), INDEX(email)
);

CREATE TABLE IF NOT EXISTS orders(
 id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 customer_id INT UNSIGNED NULL,
 customer_name VARCHAR(150) NOT NULL,
 customer_phone VARCHAR(40) NOT NULL,
 customer_email VARCHAR(190) DEFAULT '',
 total DECIMAL(12,2) NOT NULL DEFAULT 0,
 status ENUM('pendiente','pagado','enviado','completado','cancelado') DEFAULT 'pendiente',
 source VARCHAR(30) DEFAULT 'web',
 created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
 INDEX(status), INDEX(created_at)
);

CREATE TABLE IF NOT EXISTS order_items(
 id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
 order_id INT UNSIGNED NOT NULL,
 product_id INT UNSIGNED NULL,
 product_name VARCHAR(180) NOT NULL,
 quantity INT UNSIGNED NOT NULL DEFAULT 1,
 price DECIMAL(12,2) NOT NULL DEFAULT 0,
 subtotal DECIMAL(12,2) NOT NULL DEFAULT 0,
 INDEX(order_id)
);

INSERT IGNORE INTO settings(`key`,value) VALUES
('site_name','WFlix Market'),
('whatsapp','573000000000'),
('hero_title','TU MUNDO DIGITAL EN UN SOLO LUGAR'),
('hero_text','Planes, suscripciones, juegos, software y más al mejor precio.'),
('hero_button','Ver productos'),
('hero_image',''),
('meta_description','WFlix Market: marketplace digital moderno.'),
('currency','COP');

INSERT IGNORE INTO categories(name,icon,sort_order) VALUES
('Streaming','▶️',1),('Gaming','🎮',2),('Música','🎵',3),('Software','▦',4),
('Tarjetas y saldo','💳',5),('Servicios digitales','☁️',6),('Accesorios','🎧',7);

-- Crea el administrador desde crear_admin.php para elegir una contraseña segura.
