-- =============================================================
-- SEED: 03-seed-subscriptions.sql
-- DESCRIPCION: Datos de desarrollo para suscripciones
-- VERSION: 1.0.0
-- PROYECTO: ERP-Core V2
-- FECHA: 2026-01-10
-- =============================================================

-- Obtener IDs de planes (los planes ya deben existir desde el DDL)
-- Los planes fueron insertados en 05-billing-usage.sql

-- Suscripciones para tenants de desarrollo
INSERT INTO billing.tenant_subscriptions (
    id, tenant_id, plan_id, billing_cycle,
    current_period_start, current_period_end, status,
    billing_email, billing_name, tax_id,
    current_price, contracted_users, contracted_branches,
    auto_renew
)
SELECT
    'dddd1111-1111-1111-1111-111111111111'::uuid,
    '11111111-1111-1111-1111-111111111111'::uuid,
    sp.id,
    'monthly',
    CURRENT_DATE - INTERVAL '15 days',
    CURRENT_DATE + INTERVAL '15 days',
    'active',
    'billing@demo.com',
    'Empresa Demo SA de CV',
    'ABC123456XYZ',
    sp.base_monthly_price,
    10,
    5,
    TRUE
FROM billing.subscription_plans sp
WHERE sp.code = 'professional'
ON CONFLICT (tenant_id) DO NOTHING;

INSERT INTO billing.tenant_subscriptions (
    id, tenant_id, plan_id, billing_cycle,
    current_period_start, current_period_end, status,
    trial_start, trial_end,
    billing_email, billing_name, tax_id,
    current_price, contracted_users, contracted_branches,
    auto_renew
)
SELECT
    'dddd2222-2222-2222-2222-222222222222'::uuid,
    '22222222-2222-2222-2222-222222222222'::uuid,
    sp.id,
    'monthly',
    CURRENT_DATE - INTERVAL '10 days',
    CURRENT_DATE + INTERVAL '20 days',
    'trial',
    CURRENT_DATE - INTERVAL '10 days',
    CURRENT_DATE + INTERVAL '4 days',
    'billing@test.com',
    'Tienda Pruebas',
    'XYZ987654ABC',
    0, -- Gratis durante trial
    5,
    1,
    TRUE
FROM billing.subscription_plans sp
WHERE sp.code = 'starter'
ON CONFLICT (tenant_id) DO NOTHING;

-- Usage Tracking para el periodo actual
INSERT INTO billing.usage_tracking (
    id, tenant_id, period_start, period_end,
    active_users, peak_concurrent_users, active_branches,
    storage_used_gb, api_calls, sales_count, sales_amount,
    mobile_sessions, payment_transactions
)
VALUES
    -- Uso de Demo (tenant activo)
    ('eeee3333-1111-1111-1111-111111111111', '11111111-1111-1111-1111-111111111111',
     date_trunc('month', CURRENT_DATE), date_trunc('month', CURRENT_DATE) + INTERVAL '1 month' - INTERVAL '1 day',
     4, 3, 4, 2.5, 15000, 250, 125000.00, 150, 75),

    -- Uso de Test Store (trial)
    ('eeee4444-2222-2222-2222-222222222222', '22222222-2222-2222-2222-222222222222',
     date_trunc('month', CURRENT_DATE), date_trunc('month', CURRENT_DATE) + INTERVAL '1 month' - INTERVAL '1 day',
     1, 1, 1, 0.1, 500, 15, 3500.00, 10, 5)
ON CONFLICT (tenant_id, period_start) DO NOTHING;

-- Factura de ejemplo (pagada)
INSERT INTO billing.invoices (
    id, tenant_id, subscription_id, invoice_number, invoice_date,
    period_start, period_end,
    billing_name, billing_email, tax_id,
    subtotal, tax_amount, total, currency, status,
    due_date, paid_at, paid_amount, payment_method, payment_reference
)
VALUES
    ('ffff1111-1111-1111-1111-111111111111', '11111111-1111-1111-1111-111111111111',
     'dddd1111-1111-1111-1111-111111111111',
     'INV-2026-000001', CURRENT_DATE - INTERVAL '1 month',
     CURRENT_DATE - INTERVAL '2 months', CURRENT_DATE - INTERVAL '1 month',
     'Empresa Demo SA de CV', 'billing@demo.com', 'ABC123456XYZ',
     999.00, 159.84, 1158.84, 'MXN', 'paid',
     CURRENT_DATE - INTERVAL '25 days', CURRENT_DATE - INTERVAL '26 days', 1158.84,
     'card', 'ch_1234567890')
ON CONFLICT DO NOTHING;

-- Items de factura
INSERT INTO billing.invoice_items (
    id, invoice_id, description, item_type, quantity, unit_price, subtotal
)
VALUES
    (gen_random_uuid(), 'ffff1111-1111-1111-1111-111111111111',
     'Suscripcion Professional - Diciembre 2025', 'subscription', 1, 999.00, 999.00)
ON CONFLICT DO NOTHING;

-- Alertas de billing de ejemplo
INSERT INTO billing.billing_alerts (
    id, tenant_id, alert_type, title, message, severity, status
)
VALUES
    -- Alerta resuelta
    (gen_random_uuid(), '11111111-1111-1111-1111-111111111111',
     'payment_due', 'Pago pendiente procesado',
     'Su pago del periodo anterior ha sido procesado exitosamente.',
     'info', 'resolved'),

    -- Alerta de trial ending para tenant test
    (gen_random_uuid(), '22222222-2222-2222-2222-222222222222',
     'trial_ending', 'Su periodo de prueba termina pronto',
     'Su periodo de prueba terminara en 4 dias. Seleccione un plan para continuar usando el servicio.',
     'warning', 'active')
ON CONFLICT DO NOTHING;
