-- Create service type enum for consistent type handling across the application
-- This enum is shared between backend and frontend through TypeScript types

-- Drop existing constraints that might conflict
ALTER TABLE tours DROP CONSTRAINT IF EXISTS tours_type_check;
ALTER TABLE tour_recommendations DROP CONSTRAINT IF EXISTS tour_recommendations_recommended_type_check;

-- Create the service type enum
DO $$ BEGIN
  CREATE TYPE service_type AS ENUM (
    'daily_tour',
    'private_guide',
    'driver_car',
    'activity_bundle',
    'hot_air_balloon',
    'atv',
    'horse_riding',
    'guided_tour',
    'museum_tour',
    'adventure',
    'cultural',
    'nature'
  );
EXCEPTION
  WHEN duplicate_object THEN null;
END $$;

-- Update tours table to use the enum
ALTER TABLE tours 
  ALTER COLUMN type TYPE service_type USING type::service_type;

-- Update tour_recommendations table to use the enum
ALTER TABLE tour_recommendations 
  ALTER COLUMN recommended_type TYPE service_type USING recommended_type::service_type;

-- Add display metadata columns to tour_recommendations for AI-driven UI
ALTER TABLE tour_recommendations
  ADD COLUMN IF NOT EXISTS display_service_type_label TEXT,
  ADD COLUMN IF NOT EXISTS display_service_type_icon TEXT,
  ADD COLUMN IF NOT EXISTS display_group_label TEXT,
  ADD COLUMN IF NOT EXISTS display_group_description TEXT,
  ADD COLUMN IF NOT EXISTS display_segment_message TEXT;

-- Add comments for documentation
COMMENT ON TYPE service_type IS 'Service type enum shared between backend and frontend. Ensures consistency and eliminates UI-side string matching.';
COMMENT ON COLUMN tour_recommendations.display_service_type_label IS 'AI-provided label for the service type (e.g., "Günlük Tur")';
COMMENT ON COLUMN tour_recommendations.display_service_type_icon IS 'AI-provided icon/emoji for the service type (e.g., "🗓️")';
COMMENT ON COLUMN tour_recommendations.display_group_label IS 'AI-provided group label for UI grouping (e.g., "Önerilen Turlar")';
COMMENT ON COLUMN tour_recommendations.display_group_description IS 'AI-provided description for the recommendation group';
COMMENT ON COLUMN tour_recommendations.display_segment_message IS 'AI-provided personalized message based on traveler profile';