import { Schema, model } from 'mongoose';

import {
  ICompanyDoc,
  ICompanyModel,
} from '@/modules/company/company.interface';
import { paginate, toJSON } from '@/shared/utils/plugins/index';
import { Status, CompanyType } from '@/shared/constants/enum.constant';
import { transformCompanyForResponse } from '@/shared/utils/maskingHelper';

const companySchema = new Schema<ICompanyDoc>(
  {
    name: { type: String, required: true },
    gstNumber: { type: String },
    companyType: { type: String, enum: CompanyType, required: true },
    websiteUrl: { type: String },
    logo: { type: String },
    color: { type: String },
    description: { type: String },
    address: { type: String },
    area: { type: Schema.Types.ObjectId, ref: 'Area' },
    city: { type: Schema.Types.ObjectId, ref: 'City' },
    state: { type: Schema.Types.ObjectId, ref: 'State' },
    country: { type: Schema.Types.ObjectId, ref: 'Country' },
    pincode: { type: Number },
    status: {
      type: String,
      enum: Object.values(Status),
      default: Status.PENDING,
    },
    maxUserCount: { type: Number },
    planValidity: {
      validfor: { type: Number },
      validUnit: { type: String, enum: ['days', 'months', 'years'] },
    },
    planExpiryDate: { type: Date },
    price: {
      type: Number,
      default: 0,
    },
    tax: {
      type: Number,
      default: 0,
    },
    discount: {
      type: Number,
      default: 0,
    },
    finalPrice: {
      type: Number,
      default: 0,
    },
    whatsAppCredit: {
      type: Number,
      default: 0,
    },
    smsCredit: {
      type: Number,
      default: 0,
    },
    emailCredit: {
      type: Number,
      default: 0,
    },
    displayLogoOnInvoices: {
      type: Boolean,
      default: false,
    },
    displayLogoOnQuotations: {
      type: Boolean,
      default: false,
    },
    panNumber: { type: String },
    createdBy: { type: Schema.Types.ObjectId, ref: 'User' },
    updatedBy: { type: Schema.Types.ObjectId, ref: 'User' },
    whatsapp: {
      accessToken: { type: String },
      phoneNumberId: { type: String },
      businessId: { type: String },
      fbAppId: { type: String },
      fbSecretId: { type: String },
    },
    fast2sms: {
      accessToken: {
        type: String,
      },
      senderId: {
        type: String,
      },
    },
    myoperator: {
      apiToken: {
        type: String,
      },
      secretKey: {
        type: String,
      },
      xApiKey: {
        type: String,
      },
      companyId: {
        type: String,
      },
      webhookSecret: {
        type: String,
      },
      virtualMobileNumber: {
        type: String,
      },
      publicIvrId: {
        type: String,
      },
      isActive: {
        type: Boolean,
        default: false,
      },
      connectionStatus: {
        type: String,
        default: 'disconnected',
      },
      lastSyncedAt: {
        type: Date,
      },
      lastConnectionTest: {
        type: Date,
      },
    },
    defaultUOM: {
      type: String
    }
  },
  {
    timestamps: true,
  },
);

// Custom transform to mask sensitive fields
companySchema.methods.toJSON = function() {
  return transformCompanyForResponse(this, {
    maskSensitiveData: true,
    includeTimeStamps: false
  });
};

// add plugin that converts mongoose to json
companySchema.plugin(toJSON);
companySchema.plugin(paginate);

export const Company = model<ICompanyDoc, ICompanyModel>(
  'Company',
  companySchema,
);
