import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose';
import mongoose from 'mongoose';
import { User } from './user.schema';
import { Map_Shipment_Status } from './map_shipment_status.schema';
import { Shipment_Mode } from './map-shipment-mode.schema';
import { Map_Shipment_Attachment_Document } from './map-shipment-attachment';
import { ACTIVE, COMPLETED, SHIPMENT_TYPE } from 'src/utils/constants';

@Schema({ timestamps: true })
export class Shipment {
  @Prop({ type: mongoose.Types.ObjectId })
  company_id: User;

  @Prop()
  job_id: string;

  @Prop({ default: '' })
  name_en: string; //////name in english//////

  @Prop({ default: '' })
  name_ar: string; //////name in arabic//////

  // @Prop({ default: '' })
  // commodity_en: string; //////commodity in english///////

  // @Prop({ default: '' })
  // commodity_ar: string; //////commodity in arabic///////

  @Prop()
  pickup_from_en: string; //////pickup place in english/////////

  @Prop({ default: '' })
  pickup_from_ar: string; //////pickup place in arabic///////

  @Prop({ default: '' })
  delivered_to_en: string; //////delivery place in arabic///////

  @Prop({ default: '' })
  delivered_to_ar: string; //////delivery place in arabic///////

  @Prop({ type: mongoose.Types.ObjectId })
  shipment_mode: Shipment_Mode;

  @Prop({ default: '' })
  bill_of_lading: string;

  @Prop({ default: '' })
  containers: string;

  @Prop({ default: [] })
  container_number: Array<string>;

  @Prop({
    enum: [ACTIVE, COMPLETED],
    default: ACTIVE,
  })
  current_state: string;

  @Prop({ type: mongoose.Types.ObjectId })
  current_status_id: Map_Shipment_Status;

  @Prop({ type: mongoose.Types.ObjectId })
  attachment_id: [Map_Shipment_Attachment_Document];

  @Prop({ default: '' })
  vessel_name: string;

  @Prop({ default: '' })
  voyage: string;

  @Prop({ default: '' })
  person_name_agent: string;

  @Prop({ default: '' })
  agent: string;

  @Prop({ default: '' })
  shipping_line: string;

  @Prop({
    type: String,
    enum: SHIPMENT_TYPE,
    default: SHIPMENT_TYPE.GLOBAL,
  })
  shipment_type: SHIPMENT_TYPE;

  @Prop()
  eta: Date;

  @Prop()
  etd: Date;

  @Prop()
  serial_number: string;

  @Prop({ default: '' })
  verification_code: string;

  @Prop({ default: false })
  is_verified: boolean;

  @Prop()
  verified_at: Date;
}

export const ShipmentSchema = SchemaFactory.createForClass(Shipment);
export type ShipmentDocument = Shipment & Document;

// Indexes for get_all_shipments query patterns
ShipmentSchema.index({ createdAt: -1 });
ShipmentSchema.index({ current_state: 1, createdAt: -1 });
ShipmentSchema.index({ shipment_mode: 1, createdAt: -1 });
ShipmentSchema.index({ company_id: 1, createdAt: -1 });
ShipmentSchema.index({ current_state: 1, shipment_mode: 1, createdAt: -1 });
ShipmentSchema.index({ current_state: 1, company_id: 1, createdAt: -1 });
