import mongoose, { Document, Schema } from 'mongoose';

export interface ILocalizedString {
  en: string;
  zh: string;
}

const localizedStringSchema = {
  en: { type: String, default: '' },
  zh: { type: String, default: '' },
};

// --- Collection model: one document per event ---

export interface IMediaItem {
  url: string;
  type: 'image' | 'video';
}

export interface IEventGallery extends Document {
  title: ILocalizedString;
  slug: string;
  description: ILocalizedString;
  coverImage?: string;
  media: IMediaItem[];
  photos?: string[];
  eventDate: Date;
  status: 'draft' | 'published';
  cardSize: 'small' | 'wide' | 'tall' | 'large';
  order: number;
  createdAt: Date;
  updatedAt: Date;
}

const mediaItemSchema = new Schema(
  {
    url: { type: String, required: true },
    type: { type: String, enum: ['image', 'video'], default: 'image' },
  },
  { _id: false }
);

const eventGallerySchema = new Schema<IEventGallery>(
  {
    title: { type: localizedStringSchema, required: true },
    slug: { type: String, required: true, unique: true },
    description: { type: localizedStringSchema, default: { en: '', zh: '' } },
    coverImage: String,
    media: { type: [mediaItemSchema], default: [] },
    photos: [String],
    eventDate: { type: Date, default: Date.now },
    status: { type: String, enum: ['draft', 'published'], default: 'draft' },
    cardSize: { type: String, enum: ['small', 'wide', 'tall', 'large'], default: 'small' },
    order: { type: Number, default: 0 },
  },
  { timestamps: true }
);

eventGallerySchema.index({ status: 1, eventDate: -1 });

export const EventGallery = mongoose.model<IEventGallery>(
  'EventGallery',
  eventGallerySchema
);

// --- Single-document model: page-level hero settings ---

export interface IEventGallerySettings extends Document {
  heroTitle: ILocalizedString;
  heroSubtitle: ILocalizedString;
  heroImage?: string;
  updatedAt: Date;
}

const eventGallerySettingsSchema = new Schema<IEventGallerySettings>(
  {
    heroTitle: {
      type: localizedStringSchema,
      default: { en: 'Event Gallery', zh: '活動花絮' },
    },
    heroSubtitle: {
      type: localizedStringSchema,
      default: {
        en: 'Explore the highlights from our various Member Exclusive Events.',
        zh: '探索我們各類會員專屬活動的精彩花絮。',
      },
    },
    heroImage: String,
  },
  { timestamps: true }
);

export const EventGallerySettings = mongoose.model<IEventGallerySettings>(
  'EventGallerySettings',
  eventGallerySettingsSchema
);
