-- ============================================================================
-- v9c Cleanup: Feature-Konsolidierung
-- ============================================================================
-- Created: 2026-03-20
-- Purpose: Konsolidiere Export-Features (export_csv/json/zip → data_export)
--          und Import-Features (csv_import → data_import)
--
-- Idempotent: Kann mehrfach ausgeführt werden
--
-- Lessons Learned:
--   "Ein Feature für Export, nicht drei (csv/json/zip)"
-- ============================================================================

-- ============================================================================
-- 1. csv_import → data_import (FK-sicher)
-- ============================================================================
-- Zuerst Ziel-Feature-Zeile sichern, dann alle FKs umhängen, dann csv_import
-- entfernen. PK direkt per UPDATE ändern scheitert, solange tier_limits noch
-- feature_id = 'csv_import' referenziert (tier_limits_feature_id_fkey).

INSERT INTO features (id, name, description, category, limit_type, reset_period, default_limit, active)
VALUES ('data_import', 'Daten importieren', 'CSV-Import (FDDB, Apple Health) + ZIP-Backup-Import', 'import', 'count', 'monthly', 0, true)
ON CONFLICT (id) DO UPDATE SET
    name = EXCLUDED.name,
    description = EXCLUDED.description,
    category = EXCLUDED.category,
    limit_type = EXCLUDED.limit_type,
    reset_period = EXCLUDED.reset_period;

UPDATE tier_limits
SET feature_id = 'data_import'
WHERE feature_id = 'csv_import';

UPDATE user_feature_restrictions
SET feature_id = 'data_import'
WHERE feature_id = 'csv_import';

UPDATE user_feature_usage
SET feature_id = 'data_import'
WHERE feature_id = 'csv_import';

-- Widget-Gateway (Migration 041): sonst blockiert FK beim Löschen von csv_import
DO $$
BEGIN
    IF EXISTS (
        SELECT 1 FROM information_schema.tables
        WHERE table_schema = 'public' AND table_name = 'widget_feature_requirements'
    ) THEN
        UPDATE widget_feature_requirements
        SET feature_id = 'data_import'
        WHERE feature_id = 'csv_import';
    END IF;
END $$;

DELETE FROM features WHERE id = 'csv_import';

-- ============================================================================
-- 2. Remove old export_csv/json/zip features
-- ============================================================================

-- Remove tier_limits for old features
DELETE FROM tier_limits
WHERE feature_id IN ('export_csv', 'export_json', 'export_zip');

-- Remove user restrictions for old features
DELETE FROM user_feature_restrictions
WHERE feature_id IN ('export_csv', 'export_json', 'export_zip');

-- Remove usage tracking for old features
DELETE FROM user_feature_usage
WHERE feature_id IN ('export_csv', 'export_json', 'export_zip');

-- Remove old feature definitions
DELETE FROM features
WHERE id IN ('export_csv', 'export_json', 'export_zip');

-- ============================================================================
-- 3. Ensure data_export exists and is properly configured
-- ============================================================================
INSERT INTO features (id, name, description, category, limit_type, reset_period, default_limit, active)
VALUES ('data_export', 'Daten exportieren', 'CSV/JSON/ZIP Export', 'export', 'count', 'monthly', 0, true)
ON CONFLICT (id) DO UPDATE SET
    name = EXCLUDED.name,
    description = EXCLUDED.description,
    category = EXCLUDED.category,
    limit_type = EXCLUDED.limit_type,
    reset_period = EXCLUDED.reset_period;

-- ============================================================================
-- 4. data_import: in Schritt 1 angelegt; kein zweites INSERT nötig
-- ============================================================================

-- ============================================================================
-- 5. Update tier_limits for data_export (consolidate from old features)
-- ============================================================================

-- FREE tier: no export
INSERT INTO tier_limits (tier_id, feature_id, limit_value)
VALUES ('free', 'data_export', 0)
ON CONFLICT (tier_id, feature_id) DO UPDATE SET limit_value = EXCLUDED.limit_value;

-- BASIC tier: 5 exports/month
INSERT INTO tier_limits (tier_id, feature_id, limit_value)
VALUES ('basic', 'data_export', 5)
ON CONFLICT (tier_id, feature_id) DO UPDATE SET limit_value = EXCLUDED.limit_value;

-- PREMIUM tier: unlimited
INSERT INTO tier_limits (tier_id, feature_id, limit_value)
VALUES ('premium', 'data_export', NULL)
ON CONFLICT (tier_id, feature_id) DO UPDATE SET limit_value = EXCLUDED.limit_value;

-- SELFHOSTED tier: unlimited
INSERT INTO tier_limits (tier_id, feature_id, limit_value)
VALUES ('selfhosted', 'data_export', NULL)
ON CONFLICT (tier_id, feature_id) DO UPDATE SET limit_value = EXCLUDED.limit_value;

-- ============================================================================
-- 6. Update tier_limits for data_import
-- ============================================================================

-- FREE tier: no import
INSERT INTO tier_limits (tier_id, feature_id, limit_value)
VALUES ('free', 'data_import', 0)
ON CONFLICT (tier_id, feature_id) DO UPDATE SET limit_value = EXCLUDED.limit_value;

-- BASIC tier: 3 imports/month
INSERT INTO tier_limits (tier_id, feature_id, limit_value)
VALUES ('basic', 'data_import', 3)
ON CONFLICT (tier_id, feature_id) DO UPDATE SET limit_value = EXCLUDED.limit_value;

-- PREMIUM tier: unlimited
INSERT INTO tier_limits (tier_id, feature_id, limit_value)
VALUES ('premium', 'data_import', NULL)
ON CONFLICT (tier_id, feature_id) DO UPDATE SET limit_value = EXCLUDED.limit_value;

-- SELFHOSTED tier: unlimited
INSERT INTO tier_limits (tier_id, feature_id, limit_value)
VALUES ('selfhosted', 'data_import', NULL)
ON CONFLICT (tier_id, feature_id) DO UPDATE SET limit_value = EXCLUDED.limit_value;

-- ============================================================================
-- Cleanup complete
-- ============================================================================
-- Final feature list:
--   Data: weight_entries, circumference_entries, caliper_entries,
--         nutrition_entries, activity_entries, photos
--   AI: ai_calls, ai_pipeline
--   Export/Import: data_export, data_import
--
-- Total: 10 features (down from 13)
-- ============================================================================
