-- Add fields to support featured events and public calendar integration
ALTER TABLE events
ADD COLUMN IF NOT EXISTS end_time TIMESTAMP WITH TIME ZONE,
ADD COLUMN IF NOT EXISTS image_url TEXT,
ADD COLUMN IF NOT EXISTS category TEXT DEFAULT 'general',
ADD COLUMN IF NOT EXISTS is_featured BOOLEAN DEFAULT FALSE,
ADD COLUMN IF NOT EXISTS is_public BOOLEAN DEFAULT FALSE,
ADD COLUMN IF NOT EXISTS is_published BOOLEAN DEFAULT TRUE,
ADD COLUMN IF NOT EXISTS external_source TEXT; -- Track if event is from scraper

-- Add indexes for performance
CREATE INDEX IF NOT EXISTS idx_events_is_featured ON events(is_featured);
CREATE INDEX IF NOT EXISTS idx_events_is_public ON events(is_public);
CREATE INDEX IF NOT EXISTS idx_events_category ON events(category);
CREATE INDEX IF NOT EXISTS idx_events_start_time ON events(start_time);
CREATE INDEX IF NOT EXISTS idx_events_external_source ON events(external_source);

-- Add RLS policy for public events (anyone can view public events)
CREATE POLICY IF NOT EXISTS "Anyone can view public published events" ON events
  FOR SELECT USING (is_public = true AND is_published = true);

-- Update existing events to be public by default for backward compatibility
UPDATE events SET is_public = true, is_published = true WHERE is_public IS NULL;

-- Add comments for clarity
COMMENT ON COLUMN events.end_time IS 'Event end time - optional, derived from start_time if not provided';
COMMENT ON COLUMN events.image_url IS 'Featured image for the event';
COMMENT ON COLUMN events.category IS 'Event category (music, arts, community, business, food, sports, etc.)';
COMMENT ON COLUMN events.is_featured IS 'Whether event should be featured prominently';
COMMENT ON COLUMN events.is_public IS 'Whether event appears in public calendar';
COMMENT ON COLUMN events.is_published IS 'Whether event is published and visible';
COMMENT ON COLUMN events.external_source IS 'Source of external events (e.g., "scraper", "manual")';