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

export type TenantBrandingDocument = HydratedDocument<TenantBranding>

// A single email-template override. `key` identifies the template
// ('invitation', 'reset', 'digest', 'trial'); subject/body are the customer's
// edited copy. Absence of a row for a key means "use the platform default",
// which lives in the portal (canonical defaults) — we only persist overrides.
@Schema({ _id: false })
export class EmailTemplateOverride {
  @Prop({ required: true, trim: true })
  key!: string

  @Prop({ default: '' })
  subject!: string

  @Prop({ default: '' })
  body!: string
}
const EmailTemplateOverrideSchema = SchemaFactory.createForClass(EmailTemplateOverride)

// Per-tenant whitelabel branding that doesn't fit on the Tenant doc itself.
// Name + brandColor live on Tenant (source of truth); this holds the email
// template overrides. Logos / custom-domain verification will join here once
// their pipelines exist. One doc per tenant, keyed by tenantId.
@Schema({ collection: 'tenant_branding', timestamps: true })
export class TenantBranding {
  @Prop({ type: Types.ObjectId, ref: 'Tenant', required: true, unique: true, index: true })
  tenantId!: Types.ObjectId

  @Prop({ type: [EmailTemplateOverrideSchema], default: [] })
  emailTemplates!: EmailTemplateOverride[]
}

export const TenantBrandingSchema = SchemaFactory.createForClass(TenantBranding)
