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

export interface PhotoRatingDoc {
  _id: string;
  clientId: mongoose.Types.ObjectId;
  folderId: mongoose.Types.ObjectId;
  fileName: string;
  stars: number; // 0 = keine Bewertung, 1-5
  approvedForDownload: boolean;
  // Optionale, kurze Notiz des Kunden zu diesem Foto (z. B. "bitte für den
  // Druck", "diese hier ausbessern") — freiwillig, kein Pflichtfeld.
  note: string;
  // Freitext-Sammlung/Album, vom Kunden selbst vergeben (z. B. "Favoriten",
  // "Für den Druck") — rein clientseitige Sortierhilfe über die
  // Sterne-Bewertung hinaus, kein separates Modell nötig.
  collection: string;
  createdAt: Date;
}

const photoRatingSchema = new Schema<PhotoRatingDoc>(
  {
    clientId: { type: Schema.Types.ObjectId, ref: "Client", required: true, index: true },
    folderId: { type: Schema.Types.ObjectId, ref: "ClientPhotoFolder", required: true, index: true },
    fileName: { type: String, required: true },
    stars: { type: Number, default: 0, min: 0, max: 5 },
    approvedForDownload: { type: Boolean, default: false },
    note: { type: String, default: "", trim: true, maxlength: 500 },
    collection: { type: String, default: "", trim: true, maxlength: 80 },
  },
  { timestamps: true }
);

photoRatingSchema.index({ folderId: 1, fileName: 1 }, { unique: true });

export const PhotoRating =
  (models.PhotoRating as mongoose.Model<PhotoRatingDoc>) ||
  model<PhotoRatingDoc>("PhotoRating", photoRatingSchema);
