import { db } from '../client';
import { organizations, type Organization, type NewOrganization } from '@banatie/database';
import { eq } from 'drizzle-orm';

export async function getOrganizationByEmail(email: string): Promise<Organization | null> {
  const [org] = await db
    .select()
    .from(organizations)
    .where(eq(organizations.email, email))
    .limit(1);

  return org || null;
}

export async function createOrganization(data: NewOrganization): Promise<Organization> {
  const [org] = await db.insert(organizations).values(data).returning();

  return org!;
}

export async function listOrganizations(): Promise<Organization[]> {
  return db.select().from(organizations).orderBy(organizations.createdAt);
}
