-- SanfeShop - SQL inicial
CREATE DATABASE IF NOT EXISTS sanfeshop_db CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
USE sanfeshop_db;

CREATE TABLE IF NOT EXISTS sa_plans (
  id INT AUTO_INCREMENT PRIMARY KEY,
  code VARCHAR(40) NOT NULL UNIQUE,
  name VARCHAR(120) NOT NULL,
  price DECIMAL(12,2) NOT NULL DEFAULT 0,
  plan_interval ENUM('monthly','yearly') NOT NULL DEFAULT 'monthly',
  features_json JSON NULL,
  enabled TINYINT(1) NOT NULL DEFAULT 1,
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB;

CREATE TABLE IF NOT EXISTS sa_shops (
  id INT AUTO_INCREMENT PRIMARY KEY,
  owner_id INT NOT NULL,
  plan_id INT NOT NULL,
  name VARCHAR(160) NOT NULL,
  slug VARCHAR(80) NOT NULL UNIQUE,
  status ENUM('provisioning','active','suspended','closed') NOT NULL DEFAULT 'provisioning',
  created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (owner_id) REFERENCES sa_users(id),
  FOREIGN KEY (plan_id) REFERENCES sa_plans(id)
) ENGINE=InnoDB;

-- Planes por defecto
INSERT IGNORE INTO sa_plans (code, name, price, plan_interval, features_json, enabled) VALUES
('BASIC_M', 'Básico', 0, 'monthly', JSON_ARRAY('Hasta 50 productos', 'Soporte por email', 'Plantilla estándar'), 1),
('PRO_M', 'Pro Mensual', 4999, 'monthly', JSON_ARRAY('Hasta 500 productos', 'Soporte prioritario', 'Plantillas premium'), 1),
('PRO_Y', 'Pro Anual', 49999, 'yearly', JSON_ARRAY('Todo PRO', '2 meses bonificados', 'Dominio personalizado'), 1);
