import { supabase } from './supabase';

export type EventRow = {
  id: number;
  title: string;
  description: string | null;
  date: string;
  user_id: string;
};

export async function createEvent(input: {
  title: string;
  description?: string;
  date: string;
}) {
  const { data: auth } = await supabase.auth.getUser();
  if (!auth.user) throw new Error('Utilizador não autenticado');

  const { data, error } = await supabase
    .from('events')
    .insert({
      title: input.title,
      description: input.description ?? null,
      date: input.date,
    })
    .select('id, title, description, date, user_id')
    .single();

  if (error) throw error;
  return data as EventRow;
}
