import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { HydratedDocument, Types } from 'mongoose'

export type PayoutDocument = HydratedDocument<Payout>

// Partner-cut payout ledger. v1 is a COMPUTED ledger (not Stripe Connect):
// Dezky bills customers, the partner earns marginPct; a job aggregates paid
// invoices per partner per month into these records. Disbursement is out-of-band
// in v1 — these rows are the ledger of what's owed/paid.
@Schema({ collection: 'payouts', timestamps: true })
export class Payout {
  @Prop({ type: Types.ObjectId, ref: 'Partner', required: true, index: true })
  partnerId!: Types.ObjectId

  // YYYY-MM the payout covers.
  @Prop({ required: true, index: true })
  periodMonth!: string

  @Prop({ enum: ['DKK', 'EUR', 'USD'], default: 'DKK' })
  currency!: 'DKK' | 'EUR' | 'USD'

  @Prop({ type: Number, default: 0 })
  grossMrrMinor!: number

  @Prop({ type: Number, default: 0 })
  marginPct!: number

  @Prop({ type: Number, default: 0 })
  payoutMinor!: number

  @Prop({ enum: ['pending', 'paid'], default: 'pending', index: true })
  status!: 'pending' | 'paid'

  @Prop()
  paidAt?: Date
}

export const PayoutSchema = SchemaFactory.createForClass(Payout)
