import { Document, Types } from 'mongoose';
import { IPaginateModel } from '@/shared/interfaces/model.interface';
import { AuditFields } from '@/shared/types/common';
import { PaymentStatus } from './payment.helper';

// Sale Types
interface BaseSale {
  kind: 'Project' | 'Property';
  soldBy?: Types.ObjectId;
  salesAt?: Date;
  notes?: string;
}

export interface ProjectSale extends BaseSale {
  kind: 'Project';
  project: Types.ObjectId;
}

export interface PropertySale extends BaseSale {
  kind: 'Property';
  property: Types.ObjectId;
}

export type Sale = ProjectSale | PropertySale;

// Payment base type
export interface IPayment extends AuditFields {
  company: Types.ObjectId;
  unitBookingOrHold: Types.ObjectId;
  name: string;
  email: string;
  phone: number;
  contactId?: Types.ObjectId;
  sales: Sale[];
}

// ✅ Use `type` for document + model typing
export type IPaymentDoc = IPayment & Document;

export interface IPaymentModel extends IPaginateModel<IPaymentDoc> {}

export type NewCreatedPayment = IPayment &
  Partial<Pick<AuditFields, 'createdBy' | 'updatedBy'>>;
export type UpdatePaymentBody = Partial<IPayment>;

export interface UpsertPaymentTimelineInput {
  customerId: string | Types.ObjectId;
  totalAmount?: number;
  paidAmount?: number;
  company?: string  | Types.ObjectId;
  timeline?: PaymentStageInput[];
  userId?: Types.ObjectId;
  createdBy?: Types.ObjectId;
}

export interface PaymentStageInput {
  label?: string;
  dueDate?: Date;
  dueAmount?: number;
  paid?: number;
  lastPaidAt?: Date;
  bankAccountId?: string | Types.ObjectId;
  status?: PaymentStatus;
  payments?: {
    amount: number;
    receivedAt?: Date;
    bankAccountId?: string | Types.ObjectId;
    method?:
      | 'cash'
      | 'card'
      | 'upi'
      | 'neft'
      | 'rtgs'
      | 'imps'
      | 'cheque'
      | 'other';
    note?: string;
  }[];
  method?:
    | 'cash'
    | 'card'
    | 'upi'
    | 'neft'
    | 'rtgs'
    | 'imps'
    | 'cheque'
    | 'other';
}
