import { eq, desc } from "drizzle-orm";
import { db } from "@/db";
import { proposals, leads, clients, offer_macros } from "@/db/schema";
import type { AssembledProposal } from "./assemble";

export type ProposalListItem = {
  id: string;
  slug: string;
  title: string | null;
  state: string;
  leadId: string | null;
  leadName: string | null;
  clientId: string | null;
  clientName: string | null;
  offerMacroId: string;
  offerName: string;
  createdAt: Date;
  updatedAt: Date;
};

export async function listProposals(): Promise<ProposalListItem[]> {
  const rows = await db
    .select({
      id: proposals.id,
      slug: proposals.slug,
      title: proposals.title,
      state: proposals.state,
      leadId: proposals.lead_id,
      leadName: leads.name,
      clientId: proposals.client_id,
      clientName: clients.name,
      offerMacroId: proposals.offer_macro_id,
      offerName: offer_macros.public_name,
      createdAt: proposals.created_at,
      updatedAt: proposals.updated_at,
    })
    .from(proposals)
    .leftJoin(leads, eq(proposals.lead_id, leads.id))
    .leftJoin(clients, eq(proposals.client_id, clients.id))
    .leftJoin(offer_macros, eq(proposals.offer_macro_id, offer_macros.id))
    .orderBy(desc(proposals.created_at));

  return rows.map((r) => ({
    id: r.id,
    slug: r.slug,
    title: r.title,
    state: r.state,
    leadId: r.leadId,
    leadName: r.leadName,
    clientId: r.clientId,
    clientName: r.clientName,
    offerMacroId: r.offerMacroId,
    offerName: r.offerName ?? "",
    createdAt: r.createdAt,
    updatedAt: r.updatedAt,
  }));
}

export type ProposalDetail = {
  id: string;
  slug: string;
  title: string | null;
  state: string;
  leadId: string | null;
  clientId: string | null;
  offerMacroId: string;
  model: string | null;
  selectedTier: string | null;
  acceptedAt: Date | null;
  clientEmail: string | null;
  clientNotes: string | null;
  content: AssembledProposal;
  createdAt: Date;
  updatedAt: Date;
};

export async function getProposalById(id: string): Promise<ProposalDetail | null> {
  const [row] = await db
    .select()
    .from(proposals)
    .where(eq(proposals.id, id))
    .limit(1);

  if (!row) return null;
  return mapRow(row);
}

export async function getProposalBySlug(slug: string): Promise<ProposalDetail | null> {
  const [row] = await db
    .select()
    .from(proposals)
    .where(eq(proposals.slug, slug))
    .limit(1);

  if (!row) return null;
  return mapRow(row);
}

function mapRow(row: typeof proposals.$inferSelect): ProposalDetail {
  return {
    id: row.id,
    slug: row.slug,
    title: row.title,
    state: row.state,
    leadId: row.lead_id,
    clientId: row.client_id,
    offerMacroId: row.offer_macro_id,
    model: row.model,
    selectedTier: row.selected_tier,
    acceptedAt: row.accepted_at,
    clientEmail: row.client_email,
    clientNotes: row.client_notes,
    content: row.content as AssembledProposal,
    createdAt: row.created_at,
    updatedAt: row.updated_at,
  };
}
