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

export type PartnerDocument = HydratedDocument<Partner>

export type PartnerStatus = 'active' | 'in-negotiation' | 'paused' | 'terminated'

@Schema({ collection: 'partners', timestamps: true })
export class Partner {
  // URL-safe identifier, same convention as Tenant.slug.
  @Prop({ required: true, unique: true, index: true, lowercase: true, trim: true })
  slug!: string

  @Prop({ required: true, trim: true })
  name!: string

  // The partner's own organization domain (e.g. 'nordicmsp.dk'). Not used by
  // any service-level integration — purely metadata for display + contact.
  @Prop({ required: true, trim: true })
  domain!: string

  @Prop({
    enum: ['active', 'in-negotiation', 'paused', 'terminated'],
    default: 'in-negotiation',
    index: true,
  })
  status!: PartnerStatus

  // Revenue share. 20 = partner keeps 20% of their customers' MRR. One number
  // per partner — we don't negotiate per-tenant margin under a partner.
  @Prop({ default: 0, min: 0, max: 100 })
  marginPct!: number

  @Prop()
  partnershipStartedAt?: Date

  @Prop({
    type: {
      primaryName: String,
      primaryEmail: String,
      billingEmail: String,
    },
    default: {},
  })
  contactInfo!: {
    primaryName?: string
    primaryEmail?: string
    billingEmail?: string
  }

  @Prop({
    type: {
      companyName: String,
      vatId: String,
      country: String,
      contactEmail: String,
    },
    default: {},
  })
  billingInfo!: {
    companyName?: string
    vatId?: string
    country?: string
    contactEmail?: string
  }

  // Partner-editable business profile (settings → Contact info form).
  @Prop({
    type: {
      legalName: String,
      tradingName: String,
      address: String,
      country: String,
      primaryEmail: String,
      primaryPhone: String,
      supportHotline: String,
      website: String,
    },
    default: {},
  })
  profile!: {
    legalName?: string
    tradingName?: string
    address?: string
    country?: string
    primaryEmail?: string
    primaryPhone?: string
    supportHotline?: string
    website?: string
  }

  // Partner-level notification preferences — one row per event type. Partner-editable.
  @Prop({ type: [{ event: String, cadence: String, channels: [String] }], default: [] })
  notificationPrefs!: Array<{ event: string; cadence: string; channels: string[] }>

  // Reseller-agreement terms. Operator-managed — read-only to the partner.
  @Prop({
    type: {
      tier: String,
      payoutCadence: String,
      effectiveAt: Date,
      termMonths: Number,
      noticePeriodDays: Number,
      liabilityCap: String,
      governingLaw: String,
      signedBy: String,
    },
    default: {},
  })
  agreement!: {
    tier?: string
    payoutCadence?: string
    effectiveAt?: Date
    termMonths?: number
    noticePeriodDays?: number
    liabilityCap?: string
    governingLaw?: string
    signedBy?: string
  }

  // Document references (URL + metadata; bytes live in object storage later).
  // Operator-managed for v1.
  @Prop({
    type: [{ name: String, url: String, kind: String, size: String, uploadedAt: Date }],
    default: [],
  })
  documents!: Array<{ name: string; url?: string; kind?: string; size?: string; uploadedAt?: Date }>
}

export const PartnerSchema = SchemaFactory.createForClass(Partner)
