import { Document, Types } from 'mongoose';

import { IPaginateModel } from '@/shared/interfaces/model.interface';
import { Status, CompanyType } from '@/shared/constants/enum.constant';
import { AuditFields } from '@/shared/types/common';

export type CompanyFilter = {
  status?: string;
  companyType?: string;
  planExpiryDate?: {
    $gte?: Date;
    $lte?: Date;
  };
} & Record<string, unknown>;
export interface ICompanyBase {
  name: string;
  gstNumber?: string;
  companyType: CompanyType;
  websiteUrl?: string;
  logo?: string;
  color?: string;
  description?: string;
  address?: string;
  area?: Types.ObjectId;
  city?: Types.ObjectId;
  state?: Types.ObjectId;
  country?: Types.ObjectId;
  pincode?: number;
  planExpiryDate: Date;
  status?: Status;
  maxUserCount?: number;
  planValidity?: {
    validfor: number;
    validUnit: 'days' | 'months' | 'years';
  };
  price?: number;
  tax?: number;
  discount?: number;
  finalPrice?: number;
  displayLogoOnInvoices?: boolean;
  displayLogoOnQuotations?: boolean;
  panNumber?: string;
  whatsAppCredit?: number;
  smsCredit?: number;
  emailCredit?: number;
  whatsapp?: {
    accessToken?: string;
    phoneNumberId?: string;
    fbAppId?: string;
    fbSecretId?: string;
    businessId?: string;
  };
  fast2sms?: {
    accessToken?: string;
    senderId?: string;
  };
  myoperator?: {
    apiToken?: string;
    secretKey?: string;
    xApiKey?: string;
    companyId?: string;
    webhookSecret?: string;
    virtualMobileNumber?: string;
    publicIvrId?: string;
    isActive?: boolean;
    connectionStatus?: 'disconnected' | 'requested' | 'pending' | 'connected' | 'error';
    lastSyncedAt?: Date;
    lastConnectionTest?: Date;
  };
  defaultUOM?: string;
}

export interface ICompany extends ICompanyBase, AuditFields {}

export interface ICompanyDoc extends ICompany, Document {}

export interface ICompanyModel extends IPaginateModel<ICompanyDoc> {
  isCompanyNameTaken(name: string): Promise<boolean>;
}

export type NewCreatedCompany = ICompanyBase &
  Partial<Pick<AuditFields, 'createdBy' | 'updatedBy'>>;
export type UpdateCompanyBody = Partial<ICompany>;
