// sticker.js
// Copyright (C) 2022 DTP Technologies, LLC
// License: Apache-2.0

'use strict';

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const STICKER_STATUS_LIST = [
  'processing', // the sticker is in the processing queue
  'live',       // the sticker is available for use
  'rejected',   // the sticker was rejected (by proccessing queue)
  'retired',    // the sticker has been retired
];

const StickerMediaSchema = new Schema(
  {
    bucket: { type: String, required: true },
    key: { type: String, required: true },
    type: { type: String, required: true },
    size: { type: Number, required: true },
  },
  {
    _id: false,
  },
);

/*
 * The intention is for sticker ownership to be defined by forked applications
 * and implemented by things like User, CoreUser, Channel, or whatever can
 * "own" a sticker in your app.
 */
const StickerSchema = new Schema({
  created: { type: Date, default: Date.now, required: true, index: -1 },
  status: { type: String, enum: STICKER_STATUS_LIST, default: 'processing', required: true, index: 1 },
  rejectedReason: { type: String },
  ownerType: { type: String, required: true, index: 1 },
  owner: { type: Schema.ObjectId, required: true, index: 1, refPath: 'ownerType' },
  slug: { type: String, required: true, maxlength: 20, unique: true, index: 1 },
  original: { type: StickerMediaSchema, required: true, select: false },
  encoded: { type: StickerMediaSchema },
});

module.exports = mongoose.model('Sticker', StickerSchema);