/**
 * Award Model
 * Stores company achievements, certifications, recognitions, and milestones
 */

import mongoose, { Schema, Document } from 'mongoose';

// ============================================
// TYPE DEFINITIONS
// ============================================

export type AwardType =
  | 'industry-award'
  | 'certification'
  | 'recognition'
  | 'milestone'
  | 'accolade'
  | 'ranking'
  | 'accreditation'
  | 'patent'
  | 'trademark'
  | 'other';

export type AwardStatus = 'draft' | 'pending' | 'approved' | 'published' | 'archived';

export type AwardCategory =
  | 'innovation'
  | 'growth'
  | 'leadership'
  | 'customer-service'
  | 'product'
  | 'sustainability'
  | 'diversity'
  | 'technology'
  | 'marketing'
  | 'sales'
  | 'hr'
  | 'other';

// ============================================
// SUB-SCHEMA INTERFACES
// ============================================

export interface IAwardApplication {
  id: string;
  awardName: string;
  organizingBody?: string;
  submissionDate: Date;
  deadline?: Date;
  status: 'draft' | 'submitted' | 'under-review' | 'won' | 'lost' | 'withdrawn';
  submissionUrl?: string;
  notes?: string;
  documents?: string[];
}

export interface ICertificate {
  id: string;
  name: string;
  issuingOrganization: string;
  issueDate: Date;
  expiryDate?: Date;
  certificateNumber?: string;
  certificateUrl?: string;
  verificationUrl?: string;
  verified: boolean;
  verifiedAt?: Date;
}

// ============================================
// MAIN INTERFACE
// ============================================

export interface IAward extends Document {
  // A. Core Information
  title: string;
  description?: string;
  type: AwardType;
  category?: AwardCategory;
  status: AwardStatus;

  // B. Issuing Organization
  issuingOrganization: string;
  organizationLogo?: string;
  organizationUrl?: string;

  // C. Award Details
  awardDate: Date;
  receivedDate?: Date;
  expiryDate?: Date;
  isRecurring: boolean;
  recurringFrequency?: 'annual' | 'biennial' | 'quarterly';

  // D. Recognition Level
  level: 'local' | 'regional' | 'national' | 'international' | 'global';
  rank?: number;
  totalParticipants?: number;

  // E. Recipient Information
  recipientType: 'company' | 'founder' | 'employee' | 'team' | 'product' | 'service';
  recipientId?: string;
  recipientName?: string;

  // F. Documents & Media
  certificateUrl?: string;
  badgeUrl?: string;
  imageUrl?: string;
  videoUrl?: string;
  pressReleaseUrl?: string;
  documents?: string[];

  // G. Public Showcase
  featured: boolean;
  showcaseOnWebsite: boolean;
  showcaseOnMarketing: boolean;
  showcaseOrder?: number;
  publicDescription?: string;

  // H. Application Tracking
  applications?: IAwardApplication[];
  certificates?: ICertificate[];

  // I. PR Announcements
  prAnnouncementUrl?: string;
  socialMediaUrls?: string[];
  mediaCoverage?: string[];

  // J. AI Generation
  aiGenerated?: boolean;
  aiPrompt?: string;

  // K. Metadata
  tags?: string[];
  notes?: string;

  // L. Company Reference
  companyId: string;

  // Timestamps
  createdAt: Date;
  updatedAt: Date;
}

// ============================================
// SUB-SCHEMAS
// ============================================

const AwardApplicationSchema = new Schema<IAwardApplication>({
  id: { type: String, required: true },
  awardName: { type: String, required: true },
  organizingBody: String,
  submissionDate: { type: Date, required: true },
  deadline: Date,
  status: {
    type: String,
    enum: ['draft', 'submitted', 'under-review', 'won', 'lost', 'withdrawn'],
    default: 'draft'
  },
  submissionUrl: String,
  notes: String,
  documents: [String]
}, { _id: false });

const CertificateSchema = new Schema<ICertificate>({
  id: { type: String, required: true },
  name: { type: String, required: true },
  issuingOrganization: { type: String, required: true },
  issueDate: { type: Date, required: true },
  expiryDate: Date,
  certificateNumber: String,
  certificateUrl: String,
  verificationUrl: String,
  verified: { type: Boolean, default: false },
  verifiedAt: Date
}, { _id: false });

// ============================================
// MAIN SCHEMA
// ============================================

const AwardSchema = new Schema<IAward>({
  // A. Core Information
  title: {
    type: String,
    required: [true, 'Award title is required'],
    trim: true,
    maxlength: [200, 'Title cannot exceed 200 characters']
  },
  description: { type: String, trim: true },
  type: {
    type: String,
    enum: ['industry-award', 'certification', 'recognition', 'milestone', 'accolade', 'ranking', 'accreditation', 'patent', 'trademark', 'other'],
    required: [true, 'Award type is required'],
    default: 'industry-award'
  },
  category: {
    type: String,
    enum: ['innovation', 'growth', 'leadership', 'customer-service', 'product', 'sustainability', 'diversity', 'technology', 'marketing', 'sales', 'hr', 'other']
  },
  status: {
    type: String,
    enum: ['draft', 'pending', 'approved', 'published', 'archived'],
    default: 'draft'
  },

  // B. Issuing Organization
  issuingOrganization: {
    type: String,
    required: [true, 'Issuing organization is required'],
    trim: true
  },
  organizationLogo: String,
  organizationUrl: String,

  // C. Award Details
  awardDate: { type: Date, required: [true, 'Award date is required'] },
  receivedDate: Date,
  expiryDate: Date,
  isRecurring: { type: Boolean, default: false },
  recurringFrequency: {
    type: String,
    enum: ['annual', 'biennial', 'quarterly']
  },

  // D. Recognition Level
  level: {
    type: String,
    enum: ['local', 'regional', 'national', 'international', 'global'],
    default: 'national'
  },
  rank: Number,
  totalParticipants: Number,

  // E. Recipient Information
  recipientType: {
    type: String,
    enum: ['company', 'founder', 'employee', 'team', 'product', 'service'],
    default: 'company'
  },
  recipientId: String,
  recipientName: String,

  // F. Documents & Media
  certificateUrl: String,
  badgeUrl: String,
  imageUrl: String,
  videoUrl: String,
  pressReleaseUrl: String,
  documents: [String],

  // G. Public Showcase
  featured: { type: Boolean, default: false },
  showcaseOnWebsite: { type: Boolean, default: true },
  showcaseOnMarketing: { type: Boolean, default: false },
  showcaseOrder: Number,
  publicDescription: String,

  // H. Application Tracking
  applications: [AwardApplicationSchema],
  certificates: [CertificateSchema],

  // I. PR Announcements
  prAnnouncementUrl: String,
  socialMediaUrls: [String],
  mediaCoverage: [String],

  // J. AI Generation
  aiGenerated: { type: Boolean, default: false },
  aiPrompt: String,

  // K. Metadata
  tags: [String],
  notes: String,

  // L. Company Reference
  companyId: {
    type: String,
    required: [true, 'Company ID is required'],
    index: true
  }

}, {
  timestamps: true
});

// ============================================
// INDEXES
// ============================================

AwardSchema.index({ companyId: 1, status: 1 });
AwardSchema.index({ companyId: 1, type: 1 });
AwardSchema.index({ companyId: 1, awardDate: -1 });
AwardSchema.index({ companyId: 1, featured: 1 });

// ============================================
// EXPORT
// ============================================

export const Award = mongoose.model<IAward>('Award', AwardSchema);