-- =====================================================
-- SCHEMA: Templates de Productos
-- Epica: MCH-007 - Templates y Catalogos
-- Fecha: 2026-01-18
-- =====================================================

-- Nota: Usar schema catalog existente

-- ENUM: Template Provider
DO $$ BEGIN
    CREATE TYPE template_provider AS ENUM (
        'sabritas',
        'coca-cola',
        'bimbo',
        'marinela',
        'gamesa',
        'pepsi',
        'nestle',
        'generic'
    );
EXCEPTION
    WHEN duplicate_object THEN null;
END $$;

-- ENUM: Template Giro
DO $$ BEGIN
    CREATE TYPE template_giro AS ENUM (
        'abarrotes',
        'papeleria',
        'farmacia',
        'ferreteria',
        'general'
    );
EXCEPTION
    WHEN duplicate_object THEN null;
END $$;

-- TABLA: product_templates (global, sin tenant_id)
CREATE TABLE IF NOT EXISTS catalog.product_templates (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    provider template_provider NOT NULL,
    giro template_giro NOT NULL,
    category VARCHAR(50),
    sku VARCHAR(50) NOT NULL,
    name VARCHAR(150) NOT NULL,
    description TEXT,
    barcode VARCHAR(50),
    suggested_price DECIMAL(10,2) NOT NULL,
    cost_price DECIMAL(10,2),
    image_url TEXT,
    unit VARCHAR(20) DEFAULT 'pieza',
    metadata JSONB,
    active BOOLEAN DEFAULT true,
    created_at TIMESTAMPTZ DEFAULT NOW(),
    updated_at TIMESTAMPTZ DEFAULT NOW()
);

-- INDICES para product_templates
CREATE INDEX IF NOT EXISTS idx_product_templates_provider_giro
    ON catalog.product_templates(provider, giro);
CREATE INDEX IF NOT EXISTS idx_product_templates_barcode
    ON catalog.product_templates(barcode) WHERE barcode IS NOT NULL;
CREATE INDEX IF NOT EXISTS idx_product_templates_name_search
    ON catalog.product_templates USING gin(to_tsvector('spanish', name));

-- TABLA: template_imports (por tenant)
CREATE TABLE IF NOT EXISTS catalog.template_imports (
    id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
    tenant_id UUID NOT NULL REFERENCES auth.tenants(id) ON DELETE CASCADE,
    template_id UUID REFERENCES catalog.product_templates(id) ON DELETE SET NULL,
    provider template_provider,
    giro template_giro,
    products_count INTEGER DEFAULT 0,
    skipped_count INTEGER DEFAULT 0,
    metadata JSONB,
    imported_at TIMESTAMPTZ DEFAULT NOW()
);

-- INDICES para template_imports
CREATE INDEX IF NOT EXISTS idx_template_imports_tenant_date
    ON catalog.template_imports(tenant_id, imported_at DESC);

-- TRIGGER: Actualizar updated_at
CREATE OR REPLACE FUNCTION update_product_templates_updated_at()
RETURNS TRIGGER AS $$
BEGIN
    NEW.updated_at = NOW();
    RETURN NEW;
END;
$$ LANGUAGE plpgsql;

DROP TRIGGER IF EXISTS trg_product_templates_updated_at ON catalog.product_templates;
CREATE TRIGGER trg_product_templates_updated_at
    BEFORE UPDATE ON catalog.product_templates
    FOR EACH ROW
    EXECUTE FUNCTION update_product_templates_updated_at();

-- =====================================================
-- SEEDS: Productos de proveedores comunes
-- =====================================================

-- Sabritas (Botanas)
INSERT INTO catalog.product_templates (provider, giro, category, sku, name, barcode, suggested_price, unit) VALUES
('sabritas', 'abarrotes', 'Botanas', 'SAB001', 'Sabritas Original 45g', '7501011100011', 18.00, 'pieza'),
('sabritas', 'abarrotes', 'Botanas', 'SAB002', 'Sabritas Adobadas 45g', '7501011100028', 18.00, 'pieza'),
('sabritas', 'abarrotes', 'Botanas', 'SAB003', 'Sabritas Limon 45g', '7501011100035', 18.00, 'pieza'),
('sabritas', 'abarrotes', 'Botanas', 'SAB004', 'Doritos Nacho 62g', '7501011100110', 22.00, 'pieza'),
('sabritas', 'abarrotes', 'Botanas', 'SAB005', 'Doritos Flamin Hot 62g', '7501011100127', 22.00, 'pieza'),
('sabritas', 'abarrotes', 'Botanas', 'SAB006', 'Cheetos Torciditos 52g', '7501011100210', 20.00, 'pieza'),
('sabritas', 'abarrotes', 'Botanas', 'SAB007', 'Cheetos Flamin Hot 52g', '7501011100227', 20.00, 'pieza'),
('sabritas', 'abarrotes', 'Botanas', 'SAB008', 'Ruffles Queso 50g', '7501011100310', 20.00, 'pieza'),
('sabritas', 'abarrotes', 'Botanas', 'SAB009', 'Ruffles Original 50g', '7501011100327', 20.00, 'pieza'),
('sabritas', 'abarrotes', 'Botanas', 'SAB010', 'Fritos Limon y Sal 62g', '7501011100410', 18.00, 'pieza'),
('sabritas', 'abarrotes', 'Botanas', 'SAB011', 'Tostitos Salsa Verde 65g', '7501011100510', 22.00, 'pieza'),
('sabritas', 'abarrotes', 'Botanas', 'SAB012', 'Cheetos Poffs 38g', '7501011100234', 15.00, 'pieza'),
('sabritas', 'abarrotes', 'Botanas', 'SAB013', 'Sabritas Habanero 45g', '7501011100042', 18.00, 'pieza'),
('sabritas', 'abarrotes', 'Botanas', 'SAB014', 'Doritos Dinamita 62g', '7501011100134', 22.00, 'pieza'),
('sabritas', 'abarrotes', 'Botanas', 'SAB015', 'Takis Fuego 70g', '7501011100610', 25.00, 'pieza')
ON CONFLICT DO NOTHING;

-- Coca-Cola (Refrescos)
INSERT INTO catalog.product_templates (provider, giro, category, sku, name, barcode, suggested_price, unit) VALUES
('coca-cola', 'abarrotes', 'Refrescos', 'CC001', 'Coca-Cola Original 600ml', '7501055300000', 18.00, 'pieza'),
('coca-cola', 'abarrotes', 'Refrescos', 'CC002', 'Coca-Cola Original 2L', '7501055300100', 35.00, 'pieza'),
('coca-cola', 'abarrotes', 'Refrescos', 'CC003', 'Coca-Cola Original 355ml Lata', '7501055300200', 16.00, 'pieza'),
('coca-cola', 'abarrotes', 'Refrescos', 'CC004', 'Coca-Cola Sin Azucar 600ml', '7501055300010', 18.00, 'pieza'),
('coca-cola', 'abarrotes', 'Refrescos', 'CC005', 'Sprite 600ml', '7501055301000', 18.00, 'pieza'),
('coca-cola', 'abarrotes', 'Refrescos', 'CC006', 'Sprite 2L', '7501055301100', 35.00, 'pieza'),
('coca-cola', 'abarrotes', 'Refrescos', 'CC007', 'Fanta Naranja 600ml', '7501055302000', 18.00, 'pieza'),
('coca-cola', 'abarrotes', 'Refrescos', 'CC008', 'Fanta Naranja 2L', '7501055302100', 35.00, 'pieza'),
('coca-cola', 'abarrotes', 'Refrescos', 'CC009', 'Fresca 600ml', '7501055303000', 18.00, 'pieza'),
('coca-cola', 'abarrotes', 'Refrescos', 'CC010', 'Sidral Mundet 600ml', '7501055304000', 18.00, 'pieza'),
('coca-cola', 'abarrotes', 'Refrescos', 'CC011', 'Delaware Punch 600ml', '7501055305000', 18.00, 'pieza'),
('coca-cola', 'abarrotes', 'Refrescos', 'CC012', 'Powerade Mora 600ml', '7501055310000', 22.00, 'pieza'),
('coca-cola', 'abarrotes', 'Agua', 'CC013', 'Ciel Natural 600ml', '7501055320000', 12.00, 'pieza'),
('coca-cola', 'abarrotes', 'Agua', 'CC014', 'Ciel Natural 1.5L', '7501055320100', 18.00, 'pieza'),
('coca-cola', 'abarrotes', 'Jugos', 'CC015', 'Del Valle Nectar Manzana 1L', '7501055330000', 28.00, 'pieza')
ON CONFLICT DO NOTHING;

-- Bimbo (Pan)
INSERT INTO catalog.product_templates (provider, giro, category, sku, name, barcode, suggested_price, unit) VALUES
('bimbo', 'abarrotes', 'Pan', 'BIM001', 'Pan Blanco Bimbo Grande', '7441029500110', 52.00, 'pieza'),
('bimbo', 'abarrotes', 'Pan', 'BIM002', 'Pan Blanco Bimbo Chico', '7441029500111', 38.00, 'pieza'),
('bimbo', 'abarrotes', 'Pan', 'BIM003', 'Pan Integral Bimbo', '7441029500210', 55.00, 'pieza'),
('bimbo', 'abarrotes', 'Pan', 'BIM004', 'Pan Tostado Bimbo', '7441029500310', 42.00, 'pieza'),
('bimbo', 'abarrotes', 'Pan', 'BIM005', 'Pan Molido Bimbo 200g', '7441029500410', 22.00, 'pieza'),
('bimbo', 'abarrotes', 'Pan', 'BIM006', 'Tortillas de Harina Tia Rosa 10pz', '7441029510110', 35.00, 'pieza'),
('bimbo', 'abarrotes', 'Pan', 'BIM007', 'Tortillas de Harina Tia Rosa 20pz', '7441029510210', 55.00, 'pieza'),
('bimbo', 'abarrotes', 'Pan', 'BIM008', 'Medias Noches Bimbo 8pz', '7441029520110', 45.00, 'pieza'),
('bimbo', 'abarrotes', 'Pan', 'BIM009', 'Pan para Hot Dog Bimbo 8pz', '7441029520210', 48.00, 'pieza'),
('bimbo', 'abarrotes', 'Pan', 'BIM010', 'Pan para Hamburguesa Bimbo 4pz', '7441029520310', 38.00, 'pieza')
ON CONFLICT DO NOTHING;

-- Marinela (Galletas y Pastelitos)
INSERT INTO catalog.product_templates (provider, giro, category, sku, name, barcode, suggested_price, unit) VALUES
('marinela', 'abarrotes', 'Pastelitos', 'MAR001', 'Gansito Marinela', '7441029600110', 18.00, 'pieza'),
('marinela', 'abarrotes', 'Pastelitos', 'MAR002', 'Pinginos Marinela 2pz', '7441029600210', 20.00, 'pieza'),
('marinela', 'abarrotes', 'Pastelitos', 'MAR003', 'Chocorroles Marinela 2pz', '7441029600310', 18.00, 'pieza'),
('marinela', 'abarrotes', 'Pastelitos', 'MAR004', 'Submarinos Marinela', '7441029600410', 16.00, 'pieza'),
('marinela', 'abarrotes', 'Pastelitos', 'MAR005', 'Principe Chocolate 6pz', '7441029610110', 22.00, 'pieza'),
('marinela', 'abarrotes', 'Pastelitos', 'MAR006', 'Principe Vainilla 6pz', '7441029610210', 22.00, 'pieza'),
('marinela', 'abarrotes', 'Pastelitos', 'MAR007', 'Negrito Bimbo', '7441029620110', 16.00, 'pieza'),
('marinela', 'abarrotes', 'Pastelitos', 'MAR008', 'Mantecadas Bimbo 4pz', '7441029620210', 32.00, 'pieza'),
('marinela', 'abarrotes', 'Galletas', 'MAR009', 'Sponch Marinela 6pz', '7441029630110', 25.00, 'pieza'),
('marinela', 'abarrotes', 'Galletas', 'MAR010', 'Barritas de Fresa Marinela 6pz', '7441029630210', 28.00, 'pieza')
ON CONFLICT DO NOTHING;

-- Gamesa (Galletas)
INSERT INTO catalog.product_templates (provider, giro, category, sku, name, barcode, suggested_price, unit) VALUES
('gamesa', 'abarrotes', 'Galletas', 'GAM001', 'Marias Gamesa 170g', '7501000101110', 18.00, 'pieza'),
('gamesa', 'abarrotes', 'Galletas', 'GAM002', 'Marias Gamesa 340g', '7501000101210', 32.00, 'pieza'),
('gamesa', 'abarrotes', 'Galletas', 'GAM003', 'Arcoiris Gamesa 340g', '7501000102110', 35.00, 'pieza'),
('gamesa', 'abarrotes', 'Galletas', 'GAM004', 'Emperador Chocolate 117g', '7501000103110', 22.00, 'pieza'),
('gamesa', 'abarrotes', 'Galletas', 'GAM005', 'Emperador Vainilla 117g', '7501000103210', 22.00, 'pieza'),
('gamesa', 'abarrotes', 'Galletas', 'GAM006', 'Crackets Gamesa 360g', '7501000104110', 42.00, 'pieza'),
('gamesa', 'abarrotes', 'Galletas', 'GAM007', 'Saladitas Gamesa 186g', '7501000104210', 28.00, 'pieza'),
('gamesa', 'abarrotes', 'Galletas', 'GAM008', 'Florentinas Gamesa 155g', '7501000105110', 25.00, 'pieza'),
('gamesa', 'abarrotes', 'Galletas', 'GAM009', 'Chokis Gamesa 128g', '7501000106110', 28.00, 'pieza'),
('gamesa', 'abarrotes', 'Galletas', 'GAM010', 'Mamut Gamesa', '7501000107110', 12.00, 'pieza')
ON CONFLICT DO NOTHING;

-- Pepsi (Bebidas)
INSERT INTO catalog.product_templates (provider, giro, category, sku, name, barcode, suggested_price, unit) VALUES
('pepsi', 'abarrotes', 'Refrescos', 'PEP001', 'Pepsi 600ml', '7501031310100', 18.00, 'pieza'),
('pepsi', 'abarrotes', 'Refrescos', 'PEP002', 'Pepsi 2L', '7501031310200', 35.00, 'pieza'),
('pepsi', 'abarrotes', 'Refrescos', 'PEP003', 'Pepsi Black 600ml', '7501031310110', 18.00, 'pieza'),
('pepsi', 'abarrotes', 'Refrescos', 'PEP004', 'Mirinda Naranja 600ml', '7501031320100', 18.00, 'pieza'),
('pepsi', 'abarrotes', 'Refrescos', 'PEP005', 'Mirinda Manzana 600ml', '7501031320200', 18.00, 'pieza'),
('pepsi', 'abarrotes', 'Refrescos', 'PEP006', '7Up 600ml', '7501031330100', 18.00, 'pieza'),
('pepsi', 'abarrotes', 'Refrescos', 'PEP007', '7Up 2L', '7501031330200', 35.00, 'pieza'),
('pepsi', 'abarrotes', 'Agua', 'PEP008', 'Epura Natural 600ml', '7501031340100', 12.00, 'pieza'),
('pepsi', 'abarrotes', 'Agua', 'PEP009', 'Epura Natural 1.5L', '7501031340200', 18.00, 'pieza'),
('pepsi', 'abarrotes', 'Jugos', 'PEP010', 'Jumex Nectar Mango 1L', '7501031350100', 25.00, 'pieza')
ON CONFLICT DO NOTHING;

-- Nestle (Varios)
INSERT INTO catalog.product_templates (provider, giro, category, sku, name, barcode, suggested_price, unit) VALUES
('nestle', 'abarrotes', 'Lacteos', 'NES001', 'Leche Nido 360g', '7501058600110', 85.00, 'pieza'),
('nestle', 'abarrotes', 'Lacteos', 'NES002', 'Leche Nido 800g', '7501058600210', 165.00, 'pieza'),
('nestle', 'abarrotes', 'Lacteos', 'NES003', 'Leche Carnation Clavel 360g', '7501058601110', 42.00, 'pieza'),
('nestle', 'abarrotes', 'Chocolates', 'NES004', 'Carlos V 42g', '7501058610110', 15.00, 'pieza'),
('nestle', 'abarrotes', 'Chocolates', 'NES005', 'Crunch 42g', '7501058610210', 18.00, 'pieza'),
('nestle', 'abarrotes', 'Chocolates', 'NES006', 'Kit Kat 41.5g', '7501058610310', 22.00, 'pieza'),
('nestle', 'abarrotes', 'Cafe', 'NES007', 'Nescafe Clasico 42g', '7501058620110', 48.00, 'pieza'),
('nestle', 'abarrotes', 'Cafe', 'NES008', 'Nescafe Clasico 120g', '7501058620210', 115.00, 'pieza'),
('nestle', 'abarrotes', 'Cereales', 'NES009', 'Corn Flakes 500g', '7501058630110', 72.00, 'pieza'),
('nestle', 'abarrotes', 'Cereales', 'NES010', 'Zucaritas 420g', '7501058630210', 75.00, 'pieza')
ON CONFLICT DO NOTHING;

-- Productos Genericos (Papeleria)
INSERT INTO catalog.product_templates (provider, giro, category, sku, name, barcode, suggested_price, unit) VALUES
('generic', 'papeleria', 'Cuadernos', 'PAP001', 'Cuaderno Profesional 100 hojas', NULL, 35.00, 'pieza'),
('generic', 'papeleria', 'Cuadernos', 'PAP002', 'Cuaderno Profesional 200 hojas', NULL, 55.00, 'pieza'),
('generic', 'papeleria', 'Cuadernos', 'PAP003', 'Cuaderno Frances 100 hojas', NULL, 28.00, 'pieza'),
('generic', 'papeleria', 'Plumas', 'PAP004', 'Pluma BIC Cristal Azul', NULL, 8.00, 'pieza'),
('generic', 'papeleria', 'Plumas', 'PAP005', 'Pluma BIC Cristal Negra', NULL, 8.00, 'pieza'),
('generic', 'papeleria', 'Plumas', 'PAP006', 'Pluma BIC Cristal Roja', NULL, 8.00, 'pieza'),
('generic', 'papeleria', 'Lapices', 'PAP007', 'Lapiz Mirado No. 2', NULL, 5.00, 'pieza'),
('generic', 'papeleria', 'Lapices', 'PAP008', 'Lapiz Mirado No. 2 c/goma', NULL, 6.00, 'pieza'),
('generic', 'papeleria', 'Colores', 'PAP009', 'Colores Crayola 12pz', NULL, 45.00, 'pieza'),
('generic', 'papeleria', 'Colores', 'PAP010', 'Colores Crayola 24pz', NULL, 85.00, 'pieza'),
('generic', 'papeleria', 'Pegamento', 'PAP011', 'Resistol 850 100ml', NULL, 28.00, 'pieza'),
('generic', 'papeleria', 'Pegamento', 'PAP012', 'Pritt Barra 21g', NULL, 32.00, 'pieza'),
('generic', 'papeleria', 'Tijeras', 'PAP013', 'Tijeras escolares', NULL, 25.00, 'pieza'),
('generic', 'papeleria', 'Papel', 'PAP014', 'Hojas Blancas Paquete 100', NULL, 55.00, 'pieza'),
('generic', 'papeleria', 'Papel', 'PAP015', 'Hojas Blancas Paquete 500', NULL, 150.00, 'pieza')
ON CONFLICT DO NOTHING;

-- =====================================================
-- FIN DEL SCHEMA
-- =====================================================
