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

import { SubscriptionPlan } from './subscription.plan';
import { Workspace } from './workspace.model';

export enum subscription_type {
    FREE_TIRE = 'FREE_TIRE',
    BASE_PLAN = 'BASE_PLAN',
    SILVER_PLAN = 'SILVER_PLAN',
    GOLD_PLAN = 'GOLD_PLAN'
}

export enum subscription_payment_status {
    NOT_DONE = 'NOT_DONE',
    PAYMENT_INITIATED = 'PAYMENT_INITIATED',
    PAYMENT_FAILED = 'PAYMENT_FAILED',
    PAYMENT_COMPLETED = 'PAYMENT_COMPLETED',
}

@Schema({ timestamps: true })
export class Subscription extends Document {

    @Prop({ type: Types.ObjectId, ref: "workspaces", default: null })
    workspace_id: string

    @Prop({ type: Types.ObjectId, ref: "users", default: null })
    user_id: Types.ObjectId = null

    @Prop({ type: Types.ObjectId, ref: SubscriptionPlan.name, default: null })
    subscription_plan_id: Types.ObjectId;

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

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

    @Prop({ type: String, enum: subscription_type, default: subscription_type.FREE_TIRE })
    plan_type: string;

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

    @Prop({ type: Number, default: 0 })
    amount: number = 0

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

    @Prop({ type: Number, default: 0 })
    start_date: number;

    @Prop({ type: Number, default: 0 })
    end_date: number;

    @Prop({ type: Number, default: 0 })
    renewal_date: number;

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

    @Prop({ type: Boolean, default: false })
    is_phone_sub: boolean;

    @Prop({ type: Boolean, default: false })
    is_active: boolean;

    @Prop({ type: Number, default: 0 })
    chat_token_limit_left: number;

    @Prop({ type: Number, default: 0 })
    voice_duration_limit_left: number;

    @Prop({ type: Number, default: 0 })
    tele_duration_limit_left: number;

    @Prop({ type: Number, default: 0 })
    knowledgebase_limit_left: number;

    @Prop({ type: Number, default: 0 })
    agent_limit_left: number;

    @Prop({ type: Number, default: 0 })
    workspace_member_limit_left: number;

    @Prop({ type: Object, default: null })
    benifites: object;

    @Prop({ type: String, enum: subscription_payment_status, default: subscription_payment_status.NOT_DONE })
    payment_status: string;

    @Prop({ type: Number, default: () => +new Date() })
    updated_at: number;

    @Prop({ type: Number, default: () => +new Date() })
    created_at: number;
}

export const SubscriptionSchema = SchemaFactory.createForClass(Subscription);
