-- Migration: Convert meetings to events with type and assigned users
-- Run this migration manually or via drizzle-kit

-- Step 1: Rename meetings table to events
ALTER TABLE meetings RENAME TO events;

-- Step 2: Add type column
ALTER TABLE events ADD COLUMN type TEXT NOT NULL DEFAULT 'meeting';

-- Step 3: Create event_users junction table
CREATE TABLE event_users (
  id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
  event_id UUID NOT NULL REFERENCES events(id) ON DELETE CASCADE,
  user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
  assigned_at TIMESTAMP DEFAULT NOW() NOT NULL,
  UNIQUE(event_id, user_id)
);

-- Step 4: Assign existing events to their creators
INSERT INTO event_users (event_id, user_id)
SELECT id, created_by FROM events WHERE created_by IS NOT NULL;
