import { Schema, model, Document, Types } from 'mongoose'
import { ILocation, LocationSchema } from '../../utils/types'

const SingleContactSchema = new Schema(
	{
		name: {
			type: String,
			required: true
		},
		phone: {
			type: String,
			required: true
		},
		emoji: {
			type: String,
			required: true
		},
		color: {
			type: String,
			required: true
		},
	}
)

interface ISingleContact {
	_id: Types.ObjectId
	name: string
	phone: string
	emoji: string
	color: string  // '#ff673a'
}

const CommunitySchema = new Schema(
	{
		id: {
			type: String,
			required: true
		},
		name: {
			type: String,
			required: true
		},
		isUniversity: Boolean,
		emailDomain: String,
		photo: String,
		location: LocationSchema,
		locationString: String,
		locationRadius: Number,
		isRelief: Boolean,
		emergencyContacts: {
			type: [SingleContactSchema],
			default: undefined
		}
	},
	{ versionKey: false, timestamps: true }
)

export interface ICommunity extends Document {
	_id: Types.ObjectId
	id: string
	name: string
	isUniversity?: boolean
	emailDomain?: string
	photo?: string
	location?: ILocation
	locationString?: string
	locationRadius?: number
	isRelief?: boolean
	emergencyContacts?: ISingleContact[]
	createdAt: Date
	updatedAt: Date
}

export const Community = model('Community', CommunitySchema, 'communities')
