import mongoose, { Schema, models, model } from "mongoose";

export type InvoiceStatus = "draft" | "sent" | "paid";

export interface InvoiceDoc {
  _id: string;
  orderId: mongoose.Types.ObjectId;
  clientId: mongoose.Types.ObjectId;
  number: string;
  issueDate: Date;
  serviceDate: string;
  description: string;
  netAmount: number;
  vatRate: number;
  vatAmount: number;
  totalAmount: number;
  discountCode: string;
  discountAmount: number;
  manualDiscountAmount: number;
  manualDiscountNote: string;
  kleinunternehmer: boolean;
  // Schnappschuss der Rechnungsdaten zum Ausstellungszeitpunkt — eine
  // ausgestellte Rechnung darf sich nicht rückwirkend ändern, auch wenn
  // sich Geschäfts- oder Kundendaten später ändern.
  businessName: string;
  businessLegalName: string;
  businessStreet: string;
  businessPostCode: string;
  businessCity: string;
  businessCountryCode: string;
  businessTaxId: string;
  businessIban: string;
  businessBic: string;
  businessBankName: string;
  accountHolderName: string;
  clientName: string;
  // "" (kein Herr/Frau-Präfix) bei "divers" oder noch nicht gepflegter Anrede
  // - siehe lib/salutation.ts formatNameWithSalutation.
  clientSalutation: string;
  clientStreet: string;
  clientPostCode: string;
  clientCity: string;
  clientCountryCode: string;
  paymentReference: string;
  status: InvoiceStatus;
  createdAt: Date;
}

const invoiceSchema = new Schema<InvoiceDoc>(
  {
    // unique statt nur index: verhindert auf DB-Ebene, dass ein Doppelklick/
    // eine doppelte Anfrage zwei Rechnungen für denselben Auftrag anlegt
    // (die vorherige reine Lese-Prüfung in createInvoiceAction allein ist
    // nicht rennsicher).
    orderId: { type: Schema.Types.ObjectId, ref: "Order", required: true, unique: true },
    clientId: { type: Schema.Types.ObjectId, ref: "Client", required: true, index: true },
    number: { type: String, required: true, unique: true },
    issueDate: { type: Date, required: true },
    serviceDate: { type: String, default: "" },
    description: { type: String, required: true },
    netAmount: { type: Number, required: true },
    vatRate: { type: Number, default: 0 },
    vatAmount: { type: Number, default: 0 },
    totalAmount: { type: Number, required: true },
    discountCode: { type: String, default: "" },
    discountAmount: { type: Number, default: 0 },
    manualDiscountAmount: { type: Number, default: 0 },
    manualDiscountNote: { type: String, default: "" },
    kleinunternehmer: { type: Boolean, default: true },
    businessName: { type: String, default: "" },
    businessLegalName: { type: String, default: "" },
    businessStreet: { type: String, default: "" },
    businessPostCode: { type: String, default: "" },
    businessCity: { type: String, default: "" },
    businessCountryCode: { type: String, default: "DE" },
    businessTaxId: { type: String, default: "" },
    businessIban: { type: String, default: "" },
    businessBic: { type: String, default: "" },
    businessBankName: { type: String, default: "" },
    accountHolderName: { type: String, default: "" },
    clientName: { type: String, default: "" },
    clientSalutation: { type: String, default: "" },
    clientStreet: { type: String, default: "" },
    clientPostCode: { type: String, default: "" },
    clientCity: { type: String, default: "" },
    clientCountryCode: { type: String, default: "DE" },
    paymentReference: { type: String, default: "" },
    status: { type: String, enum: ["draft", "sent", "paid"], default: "sent" },
  },
  { timestamps: true }
);

export const Invoice =
  (models.Invoice as mongoose.Model<InvoiceDoc>) || model<InvoiceDoc>("Invoice", invoiceSchema);
