-- Activity Status Table Setup for n8n Integration
-- This table stores real-time activity data from various sources

-- Drop existing table if it exists
DROP TABLE IF EXISTS activity_status CASCADE;

-- Create the activity_status table
CREATE TABLE activity_status (
  id SERIAL PRIMARY KEY,

  -- Activity (Coding, Reading, etc.)
  activity_type VARCHAR(50), -- 'coding', 'listening', 'watching', 'gaming', 'reading'
  activity_details TEXT,
  activity_project VARCHAR(255),
  activity_language VARCHAR(50),
  activity_repo VARCHAR(255),

  -- Music (Spotify, Apple Music)
  music_playing BOOLEAN DEFAULT FALSE,
  music_track VARCHAR(255),
  music_artist VARCHAR(255),
  music_album VARCHAR(255),
  music_platform VARCHAR(50), -- 'spotify', 'apple'
  music_progress INTEGER, -- 0-100 (percentage)
  music_album_art TEXT, -- URL to album art

  -- Watching (YouTube, Netflix, Twitch)
  watching_title VARCHAR(255),
  watching_platform VARCHAR(50), -- 'youtube', 'netflix', 'twitch'
  watching_type VARCHAR(50), -- 'video', 'stream', 'movie', 'series'

  -- Gaming (Steam, PlayStation, Xbox, Discord)
  gaming_game VARCHAR(255),
  gaming_platform VARCHAR(50), -- 'steam', 'playstation', 'xbox', 'discord'
  gaming_status VARCHAR(50), -- 'playing', 'idle'

  -- Status (Mood & Custom Message)
  status_mood VARCHAR(10), -- emoji like '😊', '💻', '🎮', '😴'
  status_message TEXT,

  -- Timestamps
  created_at TIMESTAMP DEFAULT NOW(),
  updated_at TIMESTAMP DEFAULT NOW()
);

-- Create index for faster queries
CREATE INDEX idx_activity_status_updated_at ON activity_status(updated_at DESC);

-- Insert default row (will be updated by n8n workflows)
INSERT INTO activity_status (
  id,
  activity_type,
  activity_details,
  music_playing,
  status_mood,
  status_message
) VALUES (
  1,
  NULL,
  NULL,
  FALSE,
  '💻',
  'Getting started...'
);

-- Create function to automatically update updated_at timestamp
CREATE OR REPLACE FUNCTION update_activity_status_timestamp()
RETURNS TRIGGER AS $$
BEGIN
  NEW.updated_at = NOW();
  RETURN NEW;
END;
$$ LANGUAGE plpgsql;

-- Create trigger to call the function on UPDATE
CREATE TRIGGER trigger_update_activity_status_timestamp
BEFORE UPDATE ON activity_status
FOR EACH ROW
EXECUTE FUNCTION update_activity_status_timestamp();

-- Grant permissions (adjust as needed)
-- GRANT SELECT, INSERT, UPDATE ON activity_status TO your_app_user;
-- GRANT USAGE, SELECT ON SEQUENCE activity_status_id_seq TO your_app_user;

-- Display success message
DO $$
BEGIN
  RAISE NOTICE '✅ Activity Status table created successfully!';
  RAISE NOTICE '📝 You can now configure your n8n workflows to update this table.';
  RAISE NOTICE '🔗 See docs/N8N_INTEGRATION.md for setup instructions.';
END $$;
