CREATE TABLE IF NOT EXISTS teams_meetings (
  id SERIAL PRIMARY KEY,
  graph_event_id VARCHAR(500) NOT NULL,
  user_email VARCHAR(255) NOT NULL,
  subject VARCHAR(500),
  start_time TIMESTAMPTZ,
  end_time TIMESTAMPTZ,
  duration_minutes INTEGER,
  is_online_meeting BOOLEAN DEFAULT false,
  attendee_count INTEGER DEFAULT 0,
  client_attendee_count INTEGER DEFAULT 0,
  has_client_attendees BOOLEAN DEFAULT false,
  synced_at TIMESTAMPTZ DEFAULT NOW(),
  UNIQUE(user_email, graph_event_id)
);

CREATE TABLE IF NOT EXISTS teams_meeting_attendees (
  id SERIAL PRIMARY KEY,
  meeting_id INTEGER NOT NULL REFERENCES teams_meetings(id) ON DELETE CASCADE,
  attendee_email VARCHAR(255),
  attendee_name VARCHAR(255),
  matched_contact_id BIGINT REFERENCES contacts(id),
  matched_company_id BIGINT REFERENCES companies(id),
  created_at TIMESTAMPTZ DEFAULT NOW()
);

CREATE INDEX IF NOT EXISTS idx_teams_meetings_user_email_time ON teams_meetings(user_email, start_time);
CREATE INDEX IF NOT EXISTS idx_teams_meeting_attendees_meeting_id ON teams_meeting_attendees(meeting_id);
