// src/models/project.ts
// Copyright (C) 2025 DTP Technologies, LLC
// All Rights Reserved

import { Types, Schema, Document, model } from "mongoose";
import { IUser } from "./user.js";

export interface IProject extends Document {
  createdAt: Date;
  user: IUser | Types.ObjectId;
  name: string;
  slug: string;
  gitUrl?: string;
}

export const ProjectSchema = new Schema<IProject>({
  createdAt: { type: Date, default: Date.now, required: true },
  user: { type: Types.ObjectId, required: true, index: 1, ref: "User" },
  name: { type: String, default: "New Project", required: true },
  slug: { type: String, default: "new-project", required: true },
  gitUrl: { type: String },
});

ProjectSchema.index(
  {
    user: 1,
    slug: 1,
  },
  {
    partialFilterExpression: {
      slug: { $exists: true },
    },
    unique: true,
  },
);

ProjectSchema.index(
  {
    user: 1,
    gitUrl: 1,
  },
  {
    partialFilterExpression: {
      gitUrl: { $exists: true },
    },
    unique: true,
  },
);

export const Project = model<IProject>("Project", ProjectSchema);

export default Project;
