-- V010: Create user_preferences table for AI personalization
-- This table stores user preferences for personalized AI responses

CREATE TABLE IF NOT EXISTS user_preferences (
  id UUID PRIMARY KEY DEFAULT uuid_generate_v4(),
  user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,

  -- AI Response Style Preferences
  response_style VARCHAR(50) DEFAULT 'balanced', -- 'concise', 'detailed', 'balanced'
  tone VARCHAR(50) DEFAULT 'friendly', -- 'friendly', 'professional', 'casual', 'empathetic'
  language VARCHAR(10) DEFAULT 'en', -- ISO language code

  -- Content Preferences
  include_medical_disclaimers BOOLEAN DEFAULT TRUE,
  include_research_references BOOLEAN DEFAULT FALSE,
  focus_areas TEXT[], -- e.g., ['sleep', 'feeding', 'development']

  -- Feedback-Based Learning
  preferred_topics JSONB DEFAULT '[]'::jsonb, -- [{topic: 'sleep', weight: 0.8}, ...]
  avoided_topics JSONB DEFAULT '[]'::jsonb, -- [{topic: 'formula', reason: 'breastfeeding only'}]
  helpful_response_patterns JSONB DEFAULT '[]'::jsonb, -- Patterns from positive feedback
  unhelpful_response_patterns JSONB DEFAULT '[]'::jsonb, -- Patterns from negative feedback

  -- Interaction Preferences
  max_response_length INTEGER DEFAULT 500, -- characters
  prefer_bullet_points BOOLEAN DEFAULT FALSE,
  prefer_examples BOOLEAN DEFAULT TRUE,
  prefer_step_by_step BOOLEAN DEFAULT FALSE,

  -- Privacy & Safety
  share_data_for_improvement BOOLEAN DEFAULT TRUE,
  allow_personalization BOOLEAN DEFAULT TRUE,

  -- Metadata
  total_interactions INTEGER DEFAULT 0,
  positive_feedback_count INTEGER DEFAULT 0,
  negative_feedback_count INTEGER DEFAULT 0,
  last_updated_from_feedback TIMESTAMP,
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
);

-- Create unique constraint on user_id (one preference per user)
CREATE UNIQUE INDEX IF NOT EXISTS idx_user_preferences_user_id ON user_preferences(user_id);

-- Create index for quick lookups
CREATE INDEX IF NOT EXISTS idx_user_preferences_language ON user_preferences(language);
CREATE INDEX IF NOT EXISTS idx_user_preferences_updated_at ON user_preferences(updated_at);

-- Create trigger to update updated_at timestamp
CREATE OR REPLACE FUNCTION update_user_preferences_updated_at()
RETURNS TRIGGER AS $$
BEGIN
  NEW.updated_at = CURRENT_TIMESTAMP;
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER user_preferences_updated_at_trigger
  BEFORE UPDATE ON user_preferences
  FOR EACH ROW
  EXECUTE FUNCTION update_user_preferences_updated_at();

-- Insert default preferences for existing users
INSERT INTO user_preferences (user_id, language)
SELECT id, preferred_language
FROM users
WHERE id NOT IN (SELECT user_id FROM user_preferences)
ON CONFLICT (user_id) DO NOTHING;

COMMENT ON TABLE user_preferences IS 'Stores AI personalization preferences learned from user interactions';
COMMENT ON COLUMN user_preferences.response_style IS 'Preferred response length: concise, detailed, or balanced';
COMMENT ON COLUMN user_preferences.helpful_response_patterns IS 'Patterns extracted from positively rated responses';
COMMENT ON COLUMN user_preferences.total_interactions IS 'Total number of AI interactions for this user';
