-- Migration V018: Create multi-child UI preferences table
-- Created: 2025-10-04
-- Description: Stores user preferences for multi-child UI behavior and default selections

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

  -- View Mode Preferences
  view_mode VARCHAR(20) DEFAULT 'auto', -- 'tabs', 'cards', 'auto'

  -- Default Child Selection
  default_child_id VARCHAR(20) REFERENCES children(id) ON DELETE SET NULL,

  -- Per-Screen Last Selected Child
  last_selected_per_screen JSONB DEFAULT '{}'::jsonb,
  -- Example: {"track_feeding": "child_id", "track_sleep": "child_id", "analytics": "child_id"}

  -- Comparison Preferences
  comparison_default_children JSONB DEFAULT '[]'::jsonb,
  -- Example: ["child_id_1", "child_id_2"]

  comparison_default_metric VARCHAR(50) DEFAULT 'sleep-patterns',
  -- Options: 'sleep-patterns', 'feeding-frequency', 'growth-curves', 'activities'

  comparison_date_range VARCHAR(20) DEFAULT '7days',
  -- Options: '7days', '30days', '90days'

  -- Bulk Operation Preferences
  show_bulk_confirm BOOLEAN DEFAULT TRUE,
  remember_last_child BOOLEAN DEFAULT TRUE,

  -- Metadata
  created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
  updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,

  UNIQUE(user_id)
);

-- Create index for quick user lookups
CREATE INDEX IF NOT EXISTS idx_multi_child_preferences_user
ON multi_child_preferences(user_id);

-- Create index for default child lookups
CREATE INDEX IF NOT EXISTS idx_multi_child_preferences_default_child
ON multi_child_preferences(default_child_id)
WHERE default_child_id IS NOT NULL;

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

CREATE TRIGGER multi_child_preferences_updated_at_trigger
  BEFORE UPDATE ON multi_child_preferences
  FOR EACH ROW
  EXECUTE FUNCTION update_multi_child_preferences_updated_at();

-- Insert default preferences for existing users with 2+ children
INSERT INTO multi_child_preferences (user_id, view_mode)
SELECT DISTINCT fm.user_id, 'auto'
FROM family_members fm
INNER JOIN children c ON fm.family_id = c.family_id
WHERE c.deleted_at IS NULL
GROUP BY fm.user_id, fm.family_id
HAVING COUNT(DISTINCT c.id) >= 2
ON CONFLICT (user_id) DO NOTHING;

-- Add comments for documentation
COMMENT ON TABLE multi_child_preferences IS 'Stores user preferences for multi-child UI behavior';
COMMENT ON COLUMN multi_child_preferences.view_mode IS 'Dashboard view mode: tabs (1-3 kids), cards (4+), or auto';
COMMENT ON COLUMN multi_child_preferences.default_child_id IS 'Default child for quick actions';
COMMENT ON COLUMN multi_child_preferences.last_selected_per_screen IS 'JSON object mapping screen paths to last selected child ID';
COMMENT ON COLUMN multi_child_preferences.comparison_default_children IS 'JSON array of child IDs to compare by default';
