-- ================================================================
-- PMC Database Seeds - Initial Data
-- Version: 1.0.0
-- Date: 2025-12-08
-- ================================================================

-- ================================================================
-- TENANT PLANS
-- ================================================================

INSERT INTO auth.tenant_plans (id, name, code, description, price_monthly, price_yearly, max_users, max_clients, max_brands, max_generations_month, max_storage_bytes, max_custom_models, max_training_month, features, sort_order) VALUES
-- Starter (Free/Trial)
(
    'a0000000-0000-0000-0000-000000000001',
    'Starter',
    'starter',
    'Plan gratuito para empezar',
    0,
    0,
    3,          -- max_users
    5,          -- max_clients
    10,         -- max_brands
    100,        -- max_generations_month
    1073741824, -- max_storage_bytes (1GB)
    0,          -- max_custom_models
    0,          -- max_training_month
    '{"analytics": false, "automation": false, "api_access": false, "priority_support": false}'::JSONB,
    1
),
-- Pro
(
    'a0000000-0000-0000-0000-000000000002',
    'Pro',
    'pro',
    'Para equipos en crecimiento',
    49.00,
    470.00,
    10,          -- max_users
    25,          -- max_clients
    50,          -- max_brands
    1000,        -- max_generations_month
    10737418240, -- max_storage_bytes (10GB)
    10,          -- max_custom_models
    5,           -- max_training_month
    '{"analytics": true, "automation": true, "api_access": false, "priority_support": false}'::JSONB,
    2
),
-- Business
(
    'a0000000-0000-0000-0000-000000000003',
    'Business',
    'business',
    'Para agencias y empresas',
    149.00,
    1430.00,
    50,           -- max_users
    100,          -- max_clients
    200,          -- max_brands
    10000,        -- max_generations_month
    107374182400, -- max_storage_bytes (100GB)
    50,           -- max_custom_models
    20,           -- max_training_month
    '{"analytics": true, "automation": true, "api_access": true, "priority_support": true}'::JSONB,
    3
),
-- Enterprise
(
    'a0000000-0000-0000-0000-000000000004',
    'Enterprise',
    'enterprise',
    'Plan personalizado para grandes organizaciones',
    0, -- Custom pricing
    0,
    -1,  -- unlimited (represented as -1)
    -1,
    -1,
    -1,
    -1,
    -1,
    -1,
    '{"analytics": true, "automation": true, "api_access": true, "priority_support": true, "sla": true, "custom_integrations": true}'::JSONB,
    4
);

-- ================================================================
-- SYSTEM WORKFLOWS
-- ================================================================

INSERT INTO generation.workflows (id, name, code, description, type, input_schema, output_schema, estimated_time_seconds, is_active, is_system, tenant_id) VALUES
-- Product Photo Synthetic
(
    'w0000000-0000-0000-0000-000000000001',
    'Product Photo Synthetic',
    'product_photo_synthetic',
    'Genera fotos de producto en contexto comercial',
    'image',
    '{
        "type": "object",
        "required": ["product_description"],
        "properties": {
            "product_description": {"type": "string", "description": "Descripcion del producto"},
            "reference_image": {"type": "string", "format": "uri", "description": "URL imagen referencia"},
            "background": {"type": "string", "enum": ["white", "lifestyle", "custom"], "default": "white"},
            "style": {"type": "string", "enum": ["minimalist", "premium", "casual"], "default": "minimalist"},
            "lora_id": {"type": "string", "format": "uuid", "description": "ID del LoRA de marca"},
            "seed": {"type": "integer", "description": "Seed para reproducibilidad"}
        }
    }'::JSONB,
    '{
        "type": "object",
        "properties": {
            "images": {"type": "array", "items": {"type": "string", "format": "uri"}},
            "count": {"type": "integer"}
        }
    }'::JSONB,
    60,
    true,
    true,
    NULL
),
-- Social Media Post
(
    'w0000000-0000-0000-0000-000000000002',
    'Social Media Post',
    'social_media_post',
    'Genera imagen + copy para redes sociales',
    'composite',
    '{
        "type": "object",
        "required": ["brief", "brand_id"],
        "properties": {
            "brief": {
                "type": "object",
                "properties": {
                    "objetivo": {"type": "string"},
                    "audiencia": {"type": "string"},
                    "tono": {"type": "string"}
                }
            },
            "product_id": {"type": "string", "format": "uuid"},
            "channel": {"type": "string", "enum": ["instagram", "facebook", "linkedin", "tiktok"]},
            "format": {"type": "string", "enum": ["post", "story", "carousel"]},
            "brand_id": {"type": "string", "format": "uuid"}
        }
    }'::JSONB,
    '{
        "type": "object",
        "properties": {
            "images": {"type": "array"},
            "copy": {"type": "string"},
            "hashtags": {"type": "array", "items": {"type": "string"}}
        }
    }'::JSONB,
    90,
    true,
    true,
    NULL
),
-- Ad Variations
(
    'w0000000-0000-0000-0000-000000000003',
    'Ad Variations',
    'ad_variations',
    'Genera variaciones para A/B testing',
    'image',
    '{
        "type": "object",
        "required": ["base_image"],
        "properties": {
            "base_image": {"type": "string", "description": "Asset ID o URL de imagen base"},
            "variations_count": {"type": "integer", "minimum": 2, "maximum": 10, "default": 5},
            "variation_type": {"type": "string", "enum": ["color", "background", "composition"]}
        }
    }'::JSONB,
    '{
        "type": "object",
        "properties": {
            "variations": {"type": "array"},
            "metadata": {"type": "object"}
        }
    }'::JSONB,
    120,
    true,
    true,
    NULL
),
-- Virtual Avatar
(
    'w0000000-0000-0000-0000-000000000004',
    'Virtual Avatar',
    'virtual_avatar',
    'Genera avatar/influencer virtual consistente',
    'image',
    '{
        "type": "object",
        "required": ["character_lora_id"],
        "properties": {
            "character_lora_id": {"type": "string", "format": "uuid"},
            "pose": {"type": "string", "enum": ["standing", "sitting", "walking", "custom"]},
            "outfit": {"type": "string"},
            "background": {"type": "string"},
            "expression": {"type": "string", "enum": ["happy", "serious", "surprised", "neutral"]}
        }
    }'::JSONB,
    '{
        "type": "object",
        "properties": {
            "images": {"type": "array"},
            "character_consistency_score": {"type": "number"}
        }
    }'::JSONB,
    90,
    true,
    true,
    NULL
);

-- ================================================================
-- DEMO TENANT (for development)
-- ================================================================

-- Only insert demo data in development
DO $$
BEGIN
    -- Demo Tenant
    INSERT INTO auth.tenants (id, name, slug, plan_id, status, trial_ends_at) VALUES
    (
        'd0000000-0000-0000-0000-000000000001',
        'Demo Agency',
        'demo-agency',
        'a0000000-0000-0000-0000-000000000002', -- Pro plan
        'active',
        (CURRENT_DATE + INTERVAL '30 days')::DATE
    );

    -- Demo User (password: demo123456)
    INSERT INTO auth.users (id, tenant_id, email, password_hash, first_name, last_name, role, status) VALUES
    (
        'u0000000-0000-0000-0000-000000000001',
        'd0000000-0000-0000-0000-000000000001',
        'demo@pmc.dev',
        '$2b$12$LQv3c1yqBWVHxkd0LHAkCOYz6TtxMQJqhN8/X4.E5F5u5V5V5V5V5V', -- bcrypt hash of 'demo123456'
        'Demo',
        'User',
        'owner',
        'active'
    );

    -- Demo Client
    INSERT INTO crm.clients (id, tenant_id, name, slug, type, industry) VALUES
    (
        'c0000000-0000-0000-0000-000000000001',
        'd0000000-0000-0000-0000-000000000001',
        'Acme Corporation',
        'acme-corp',
        'company',
        'Technology'
    );

    -- Demo Brand
    INSERT INTO crm.brands (id, tenant_id, client_id, name, slug, primary_color, secondary_color) VALUES
    (
        'b0000000-0000-0000-0000-000000000001',
        'd0000000-0000-0000-0000-000000000001',
        'c0000000-0000-0000-0000-000000000001',
        'Acme Brand',
        'acme-brand',
        '#3B82F6',
        '#10B981'
    );

EXCEPTION WHEN OTHERS THEN
    RAISE NOTICE 'Demo data already exists or error: %', SQLERRM;
END $$;
