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

export enum CallMode {
    AUDIO = 'AUDIO',
    VIDEO = 'VIDEO',
}

export enum CallType {
    ONE_TO_ONE = "ONE_TO_ONE",
    GROUP = "GROUP",
}

export enum CallingStatus {
    ACCEPT = 'ACCEPT', // when call pic by the user
    DECLINE = 'DECLINE', // when call decline by the user
    NOT_ANSWERE = 'NOT_ANSWERE',  // when call not answered by the user
    END = 'END',  // when call end by the user
    PENDING = 'PENDING',  // when call cut at mid by the user
}


@Schema({
    timestamps: {
        createdAt: 'created_at',
        updatedAt: 'updated_at',
    },
})
export class Calling {
    @Prop({ type: String, enum: CallMode, default: CallMode.AUDIO })
    call_mode: CallMode;
    
    @Prop({ type: String, enum: CallType, default: CallType.ONE_TO_ONE })
    call_type: CallType;

    @Prop({ type: String, enum: CallingStatus, default: CallingStatus.PENDING })
    status: CallingStatus;

    @Prop({ type: Types.ObjectId, default: null, ref: 'users' })
    call_by: Types.ObjectId;

    @Prop({ type: Types.ObjectId, default: null, ref: 'users' })
    call_to: Types.ObjectId;

    @Prop({ type: Types.ObjectId, default: null, ref: 'connections' })
    connection_id: Types.ObjectId; // or group_id
    
    @Prop({ type: String, default: null })
    agora_token: string;

    @Prop({ type: String, default: null })
    channel_name: string;

    @Prop({ type: Number, default: null })
    start_time: number;

    @Prop({ type: Number, default: null })
    end_time: number;

    @Prop({ type: Number, default: null })
    duration_in_ms: number; // save call duration in milliseconds

    @Prop({ type: Number, default: null })
    total_duration_in_ms: number; // save call duration in milliseconds

    @Prop({ type: Boolean, default: false })
    is_deleted: boolean;
    
    @Prop({ type: Number, default: null })
    deleted_at: number;
}

export type CallingDocument = HydratedDocument<Calling>;
export const CallingSchema = SchemaFactory.createForClass(Calling);
