-- ============================================================================
-- OrbiQuant IA - Trading Platform
-- Schema: auth
-- File: tables/02-user_profiles.sql
-- Description: Extended user profile information
-- ============================================================================

CREATE TABLE auth.user_profiles (
  -- Primary Key
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),

  -- Foreign Key to Users
  user_id UUID NOT NULL UNIQUE,

  -- Personal Information
  first_name VARCHAR(100),
  last_name VARCHAR(100),
  display_name VARCHAR(200),
  avatar_url TEXT,
  bio TEXT,

  -- Localization
  language VARCHAR(10) DEFAULT 'en',
  timezone VARCHAR(50) DEFAULT 'UTC',
  country_code VARCHAR(2),

  -- Preferences
  newsletter_subscribed BOOLEAN NOT NULL DEFAULT false,
  marketing_emails_enabled BOOLEAN NOT NULL DEFAULT false,
  notifications_enabled BOOLEAN NOT NULL DEFAULT true,

  -- Metadata
  metadata JSONB DEFAULT '{}'::jsonb,

  -- Audit Fields
  created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
  updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),

  -- Foreign Key Constraints
  CONSTRAINT fk_user_profiles_user FOREIGN KEY (user_id)
    REFERENCES auth.users(id)
    ON DELETE CASCADE
);

-- Indexes for Performance
CREATE INDEX idx_user_profiles_user_id ON auth.user_profiles(user_id);
CREATE INDEX idx_user_profiles_display_name ON auth.user_profiles(display_name);
CREATE INDEX idx_user_profiles_country ON auth.user_profiles(country_code);
CREATE INDEX idx_user_profiles_metadata ON auth.user_profiles USING gin(metadata);

-- Table Comments
COMMENT ON TABLE auth.user_profiles IS 'Extended user profile information and preferences';

-- Column Comments
COMMENT ON COLUMN auth.user_profiles.id IS 'Unique identifier for the profile';
COMMENT ON COLUMN auth.user_profiles.user_id IS 'Reference to the user account';
COMMENT ON COLUMN auth.user_profiles.first_name IS 'User first name';
COMMENT ON COLUMN auth.user_profiles.last_name IS 'User last name';
COMMENT ON COLUMN auth.user_profiles.display_name IS 'Public display name';
COMMENT ON COLUMN auth.user_profiles.avatar_url IS 'URL to user avatar image';
COMMENT ON COLUMN auth.user_profiles.bio IS 'User biography or description';
COMMENT ON COLUMN auth.user_profiles.language IS 'Preferred language (ISO 639-1 code)';
COMMENT ON COLUMN auth.user_profiles.timezone IS 'User timezone (IANA timezone database)';
COMMENT ON COLUMN auth.user_profiles.country_code IS 'Country code (ISO 3166-1 alpha-2)';
COMMENT ON COLUMN auth.user_profiles.newsletter_subscribed IS 'Newsletter subscription preference';
COMMENT ON COLUMN auth.user_profiles.marketing_emails_enabled IS 'Marketing emails preference';
COMMENT ON COLUMN auth.user_profiles.notifications_enabled IS 'In-app notifications preference';
COMMENT ON COLUMN auth.user_profiles.metadata IS 'Additional profile metadata as JSON';
COMMENT ON COLUMN auth.user_profiles.created_at IS 'Timestamp when profile was created';
COMMENT ON COLUMN auth.user_profiles.updated_at IS 'Timestamp when profile was last updated';
