import mongoose, { Model } from 'mongoose';

import { MODEL_NAMES } from './_models';

export interface IOrganization {
  name: string;
  subfolder: string;
  isDefault?: boolean;
}

export type IOrganizationStringified = {
  [K in keyof IOrganization]: K extends '_id' | 'productId' ? string : IOrganization[K];
};

export type TOrganizationModel = Model<IOrganization>;

export type TOrganizationDoc = mongoose.HydratedDocument<IOrganization>

export interface IOrganizationLean extends IOrganization {
  _id: mongoose.Types.ObjectId;
  createdAt: Date;
  updatedAt: Date;
}

export interface IOrganizationPlain extends IOrganization {
  _id: string;
  createdAt: Date;
  updatedAt: Date;
}

export const OrganizationSchema = new mongoose.Schema<IOrganization, TOrganizationModel>(
  {
    name: {
      type: String,
      required: true,
      trim: true,
    },
    subfolder: {
      type: String,
      required: false,
      trim: true,
    },
    isDefault: {
      type: Boolean,
    },
  },
  {
    timestamps: { createdAt: true, updatedAt: false },
  },
);

export const Organization = mongoose.model<IOrganization, TOrganizationModel>(MODEL_NAMES.organizations, OrganizationSchema);
