-- WireAdmin ERP Phase 0 Schema
-- Prefix: wa_

CREATE TABLE IF NOT EXISTS wa_plans (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(50) NOT NULL,
    code VARCHAR(20) UNIQUE NOT NULL, -- 'FREE', 'PRO', 'ENTERPRISE'
    price DECIMAL(10,2) DEFAULT 0.00,
    features_json JSON,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS wa_companies (
    id INT AUTO_INCREMENT PRIMARY KEY,
    rut VARCHAR(20) UNIQUE NOT NULL, -- Chilean RUT
    business_name VARCHAR(255) NOT NULL,
    fantasy_name VARCHAR(255),
    plan_id INT,
    status ENUM('active', 'suspended', 'pending') DEFAULT 'pending',
    logo_url VARCHAR(255),
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
    FOREIGN KEY (plan_id) REFERENCES wa_plans(id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS wa_users (
    id INT AUTO_INCREMENT PRIMARY KEY,
    company_id INT, -- NULL for SuperAdmin
    email VARCHAR(150) UNIQUE NOT NULL,
    password_hash VARCHAR(255) NOT NULL,
    full_name VARCHAR(150) NOT NULL,
    role VARCHAR(50) DEFAULT 'user', -- 'owner', 'accountant', 'staff'
    is_superadmin BOOLEAN DEFAULT FALSE,
    status ENUM('active', 'inactive') DEFAULT 'active',
    last_login TIMESTAMP NULL,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    FOREIGN KEY (company_id) REFERENCES wa_companies(id) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS wa_modules (
    id INT AUTO_INCREMENT PRIMARY KEY,
    code VARCHAR(50) UNIQUE NOT NULL, -- 'billing', 'accounting', 'hr'
    name VARCHAR(100) NOT NULL,
    description TEXT,
    is_core BOOLEAN DEFAULT FALSE,
    price_monthly DECIMAL(10,2) DEFAULT 0.00,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

CREATE TABLE IF NOT EXISTS wa_company_modules (
    id INT AUTO_INCREMENT PRIMARY KEY,
    company_id INT NOT NULL,
    module_id INT NOT NULL,
    status ENUM('active', 'inactive', 'trial') DEFAULT 'active',
    activated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
    expires_at TIMESTAMP NULL,
    FOREIGN KEY (company_id) REFERENCES wa_companies(id) ON DELETE CASCADE,
    FOREIGN KEY (module_id) REFERENCES wa_modules(id) ON DELETE CASCADE,
    UNIQUE KEY unique_company_module (company_id, module_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

-- Initial Seed Data
INSERT INTO wa_plans (name, code, price) VALUES 
('Plan Gratuito', 'FREE', 0.00),
('Plan Emprendedor', 'STARTUP', 29990),
('Plan Pyme', 'SMB', 59990);

INSERT INTO wa_modules (code, name, is_core, price_monthly) VALUES 
('core', 'Núcleo Administrativo', TRUE, 0),
('accounting', 'Contabilidad Pro', FALSE, 15000),
('billing', 'Facturación Electrónica', FALSE, 25000),
('hr', 'Remuneraciones', FALSE, 20000);
