import mongoose, { Schema, Document } from 'mongoose';

export interface ICompanyDocument extends Document {
  _id: any;
  name: string;
  code: string;
  taxCode?: string;
  address?: string;
  phone?: string;
  email?: string;
  status: 'active' | 'inactive';
  createdAt: Date;
  updatedAt: Date;
}

const companySchema = new Schema<ICompanyDocument>(
  {
    _id: { type: Schema.Types.Mixed, default: () => new mongoose.Types.ObjectId().toString() }, // Support both string demo IDs and auto-gen string IDs
    name: { type: String, required: true },
    code: { type: String, required: true, unique: true, uppercase: true },
    taxCode: String,
    address: String,
    phone: String,
    email: String,
    status: { type: String, enum: ['active', 'inactive'], default: 'active' },
  },
  {
    timestamps: true,
  }
);

companySchema.index({ status: 1 });

export const Company = mongoose.model<ICompanyDocument>('Company', companySchema);
