-- Test-Daten für Resource Pool System
-- Generiert für AccountForger v2-Docker
-- Stand: 2025-06-09

-- ====================================
-- Test-Domains (500 Stück)
-- ====================================

-- Verfügbare Domains
INSERT INTO resource_pools (resource_type, resource_value, status, notes) VALUES
('domain', 'example-shop-001.com', 'available', 'Premium Domain'),
('domain', 'best-deals-online.net', 'available', 'E-Commerce Domain'),
('domain', 'super-store-24.com', 'available', 'Shop Domain'),
('domain', 'mega-market-place.org', 'available', 'Marketplace Domain'),
('domain', 'discount-heaven.net', 'available', 'Discount Domain'),
('domain', 'fashion-outlet-now.com', 'available', 'Fashion Domain'),
('domain', 'tech-gadgets-pro.net', 'available', 'Tech Domain'),
('domain', 'home-decor-style.com', 'available', 'Home Domain'),
('domain', 'sports-gear-central.net', 'available', 'Sports Domain'),
('domain', 'beauty-products-24.com', 'available', 'Beauty Domain');

-- Weitere Domains mit verschiedenen Patterns
DO $$
BEGIN
    FOR i IN 11..400 LOOP
        INSERT INTO resource_pools (resource_type, resource_value, status)
        VALUES ('domain', 
                CASE 
                    WHEN i % 5 = 0 THEN 'shop-' || i || '-online.com'
                    WHEN i % 5 = 1 THEN 'store-' || i || '-pro.net'
                    WHEN i % 5 = 2 THEN 'market-' || i || '-24.org'
                    WHEN i % 5 = 3 THEN 'outlet-' || i || '-deals.com'
                    ELSE 'commerce-' || i || '-now.net'
                END,
                'available');
    END LOOP;
END $$;

-- Einige zugeteilte Domains (50 Stück)
DO $$
BEGIN
    FOR i IN 401..450 LOOP
        INSERT INTO resource_pools (resource_type, resource_value, status, allocated_to_license, status_changed_at, status_changed_by)
        VALUES ('domain', 
                'allocated-domain-' || i || '.com',
                'allocated',
                (i % 10) + 1,  -- Zuweisung zu Lizenzen 1-10
                NOW() - INTERVAL '30 days' * RANDOM(),
                'admin');
    END LOOP;
END $$;

-- Domains in Quarantäne (50 Stück)
DO $$
BEGIN
    FOR i IN 451..500 LOOP
        INSERT INTO resource_pools (resource_type, resource_value, status, quarantine_reason, quarantine_until, notes)
        VALUES ('domain', 
                'quarantine-domain-' || i || '.com',
                'quarantine',
                CASE i % 5
                    WHEN 0 THEN 'abuse'
                    WHEN 1 THEN 'defect'
                    WHEN 2 THEN 'blacklisted'
                    WHEN 3 THEN 'expired'
                    ELSE 'review'
                END,
                NOW() + INTERVAL '7 days' + INTERVAL '1 day' * (i % 14),
                'Automatisch in Quarantäne versetzt');
    END LOOP;
END $$;

-- ====================================
-- Test IPv4-Adressen (200 Stück)
-- ====================================

-- Verfügbare IPv4-Adressen
DO $$
BEGIN
    FOR i IN 1..150 LOOP
        INSERT INTO resource_pools (resource_type, resource_value, status, notes)
        VALUES ('ipv4', 
                '192.168.' || (i / 256 + 1)::INT || '.' || (i % 256),
                'available',
                CASE 
                    WHEN i % 10 = 0 THEN 'Premium IP'
                    WHEN i % 20 = 0 THEN 'Dedicated Server IP'
                    ELSE 'Standard IP'
                END);
    END LOOP;
END $$;

-- Zugeteilte IPv4-Adressen (30 Stück)
DO $$
BEGIN
    FOR i IN 151..180 LOOP
        INSERT INTO resource_pools (resource_type, resource_value, status, allocated_to_license, status_changed_at, status_changed_by)
        VALUES ('ipv4', 
                '10.0.' || ((i-150) / 256)::INT || '.' || ((i-150) % 256),
                'allocated',
                ((i-150) % 15) + 1,
                NOW() - INTERVAL '60 days' * RANDOM(),
                'system');
    END LOOP;
END $$;

-- IPv4 in Quarantäne (20 Stück)
DO $$
BEGIN
    FOR i IN 181..200 LOOP
        INSERT INTO resource_pools (resource_type, resource_value, status, quarantine_reason, quarantine_until, notes)
        VALUES ('ipv4', 
                '172.16.' || ((i-180) / 256)::INT || '.' || ((i-180) % 256),
                'quarantine',
                CASE (i-180) % 4
                    WHEN 0 THEN 'blacklisted'
                    WHEN 1 THEN 'abuse'
                    WHEN 2 THEN 'maintenance'
                    ELSE 'defect'
                END,
                NOW() + INTERVAL '3 days' + INTERVAL '1 day' * ((i-180) % 7),
                'IP wurde gemeldet oder ist in Wartung');
    END LOOP;
END $$;

-- ====================================
-- Test Telefonnummern (100 Stück)
-- ====================================

-- Verfügbare Telefonnummern
DO $$
BEGIN
    FOR i IN 1..70 LOOP
        INSERT INTO resource_pools (resource_type, resource_value, status, notes)
        VALUES ('phone', 
                '+49' || (1500000000 + i),
                'available',
                CASE 
                    WHEN i % 5 = 0 THEN 'Premium Nummer'
                    WHEN i % 10 = 0 THEN 'Vanity Nummer'
                    ELSE 'Standard Nummer'
                END);
    END LOOP;
END $$;

-- Zugeteilte Telefonnummern (20 Stück)
DO $$
BEGIN
    FOR i IN 71..90 LOOP
        INSERT INTO resource_pools (resource_type, resource_value, status, allocated_to_license, status_changed_at, status_changed_by)
        VALUES ('phone', 
                '+49' || (1600000000 + i),
                'allocated',
                ((i-70) % 10) + 1,
                NOW() - INTERVAL '45 days' * RANDOM(),
                'admin');
    END LOOP;
END $$;

-- Telefonnummern in Quarantäne (10 Stück)
DO $$
BEGIN
    FOR i IN 91..100 LOOP
        INSERT INTO resource_pools (resource_type, resource_value, status, quarantine_reason, quarantine_until, notes)
        VALUES ('phone', 
                '+49' || (1700000000 + i),
                'quarantine',
                CASE (i-90) % 3
                    WHEN 0 THEN 'expired'
                    WHEN 1 THEN 'defect'
                    ELSE 'review'
                END,
                NOW() + INTERVAL '5 days' + INTERVAL '1 day' * ((i-90) % 5),
                'Nummer wurde zurückgegeben oder ist defekt');
    END LOOP;
END $$;

-- ====================================
-- Resource History für einige Ressourcen
-- ====================================

-- Historie für einige Domains
INSERT INTO resource_history (resource_id, license_id, action, action_by, details, ip_address)
SELECT 
    id,
    allocated_to_license,
    'allocated',
    'system',
    '{"reason": "Neue Lizenz erstellt", "customer": "Test Customer"}'::jsonb,
    '192.168.1.100'
FROM resource_pools 
WHERE status = 'allocated' 
AND resource_type = 'domain'
LIMIT 10;

-- Historie für Quarantäne
INSERT INTO resource_history (resource_id, action, action_by, details, ip_address)
SELECT 
    id,
    'quarantined',
    'admin',
    jsonb_build_object('reason', quarantine_reason, 'duration', '7 days'),
    '192.168.1.101'
FROM resource_pools 
WHERE status = 'quarantine'
LIMIT 20;

-- ====================================
-- Resource Metrics für Performance-Tracking
-- ====================================

-- Metriken für die letzten 30 Tage
DO $$
DECLARE
    resource_rec RECORD;
    days_back INT;
BEGIN
    -- Für jede Ressource, die allocated ist
    FOR resource_rec IN 
        SELECT id FROM resource_pools WHERE status = 'allocated' LIMIT 50
    LOOP
        -- Generiere Metriken für die letzten 30 Tage
        FOR days_back IN 0..29 LOOP
            INSERT INTO resource_metrics (
                resource_id, 
                metric_date, 
                usage_count, 
                performance_score, 
                cost, 
                revenue, 
                issues_count, 
                availability_percent
            ) VALUES (
                resource_rec.id,
                CURRENT_DATE - INTERVAL '1 day' * days_back,
                FLOOR(RANDOM() * 100 + 50),  -- 50-150 Nutzungen
                ROUND((RANDOM() * 40 + 60)::numeric, 2),  -- 60-100 Score
                ROUND((RANDOM() * 5 + 2)::numeric, 2),  -- 2-7 EUR Kosten
                ROUND((RANDOM() * 20 + 10)::numeric, 2),  -- 10-30 EUR Umsatz
                FLOOR(RANDOM() * 3),  -- 0-2 Issues
                ROUND((RANDOM() * 5 + 95)::numeric, 2)  -- 95-100% Verfügbarkeit
            ) ON CONFLICT (resource_id, metric_date) DO NOTHING;
        END LOOP;
    END LOOP;
END $$;

-- ====================================
-- License Resources Zuordnungen
-- ====================================

-- Verbinde einige bestehende Lizenzen mit Ressourcen
DO $$
DECLARE
    license_rec RECORD;
    domain_id INT;
    ipv4_id INT;
    phone_id INT;
BEGIN
    -- Für die ersten 10 aktiven Lizenzen
    FOR license_rec IN 
        SELECT id FROM licenses WHERE is_active = TRUE LIMIT 10
    LOOP
        -- Hole eine verfügbare Domain
        SELECT id INTO domain_id FROM resource_pools 
        WHERE resource_type = 'domain' AND status = 'available' 
        ORDER BY RANDOM() LIMIT 1;
        
        IF domain_id IS NOT NULL THEN
            -- Weise die Domain zu
            UPDATE resource_pools 
            SET status = 'allocated', 
                allocated_to_license = license_rec.id,
                status_changed_at = NOW(),
                status_changed_by = 'migration'
            WHERE id = domain_id;
            
            -- Erstelle Zuordnung
            INSERT INTO license_resources (license_id, resource_id, assigned_by)
            VALUES (license_rec.id, domain_id, 'migration');
        END IF;
        
        -- Wiederhole für IPv4
        SELECT id INTO ipv4_id FROM resource_pools 
        WHERE resource_type = 'ipv4' AND status = 'available' 
        ORDER BY RANDOM() LIMIT 1;
        
        IF ipv4_id IS NOT NULL THEN
            UPDATE resource_pools 
            SET status = 'allocated', 
                allocated_to_license = license_rec.id,
                status_changed_at = NOW(),
                status_changed_by = 'migration'
            WHERE id = ipv4_id;
            
            INSERT INTO license_resources (license_id, resource_id, assigned_by)
            VALUES (license_rec.id, ipv4_id, 'migration');
        END IF;
    END LOOP;
END $$;

-- Zusammenfassung ausgeben
DO $$
DECLARE
    domain_count INT;
    ipv4_count INT;
    phone_count INT;
BEGIN
    SELECT COUNT(*) INTO domain_count FROM resource_pools WHERE resource_type = 'domain';
    SELECT COUNT(*) INTO ipv4_count FROM resource_pools WHERE resource_type = 'ipv4';
    SELECT COUNT(*) INTO phone_count FROM resource_pools WHERE resource_type = 'phone';
    
    RAISE NOTICE 'Test-Daten erfolgreich eingefügt:';
    RAISE NOTICE '- Domains: %', domain_count;
    RAISE NOTICE '- IPv4-Adressen: %', ipv4_count;
    RAISE NOTICE '- Telefonnummern: %', phone_count;
    RAISE NOTICE '';
    RAISE NOTICE 'Status-Verteilung:';
    RAISE NOTICE '- Verfügbar: % Ressourcen', (SELECT COUNT(*) FROM resource_pools WHERE status = 'available');
    RAISE NOTICE '- Zugeteilt: % Ressourcen', (SELECT COUNT(*) FROM resource_pools WHERE status = 'allocated');
    RAISE NOTICE '- Quarantäne: % Ressourcen', (SELECT COUNT(*) FROM resource_pools WHERE status = 'quarantine');
END $$;