import { Prop, Schema, SchemaFactory } from "@nestjs/mongoose";
import { ChatHistory } from "../chat-history/chat-history.model";
import mongoose from "mongoose";
import { User } from "../users/users.model";
import { ImageHistory } from "../image-history/model/image-history.model";

export type FeedbackDocument = Feedback & Document;

@Schema({ timestamps: true, versionKey: false, _id: false})
export class Comment {
    @Prop({ type: mongoose.Schema.Types.ObjectId, required: true, ref: 'User' })
    user: User;

    @Prop({ type: String, required: true })
    comment: string;
}

@Schema({ timestamps: true })
export class Feedback {
    _id?: string | mongoose.Types.ObjectId;

    @Prop({
        type: mongoose.Schema.Types.ObjectId,
        required: [true, 'User is required'],
        ref: 'User',
    })
    user: User;

    @Prop({ type: mongoose.Schema.Types.ObjectId })
    messageId: string;

    @Prop({ type: mongoose.Schema.Types.ObjectId })
    partId: string;

    @Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'ChatHistory', default: null})
    conversationId: ChatHistory;

    @Prop({
        type: String, required: false,
        validate: {
            validator: function (this: any) {
                return this.feedback || (this.tags && this.tags.length > 0);
            },
            message: 'Either feedback or tags must be provided.',
        },
    })
    feedback: string;

    @Prop({ type: mongoose.Schema.Types.ObjectId, ref: 'ImageHistory', default: null})
    imageHistoryId: ImageHistory;


    @Prop({ type: String, required: false })
    imageId: string;

    @Prop({ type: String, required: true, enum: ['image', 'chat'] })
    type: 'image' | 'chat';

    @Prop({ type: String})
    prompt: string;


    @Prop({
        type: [String], required: false,
        validate: {
            validator: function (this: any) {
                return this.feedback || (this.tags && this.tags.length > 0);
            },
            message: 'Either feedback or tags must be provided.',
        },
    })
    tags: string[];

    @Prop({ type: String, enum: ['Open', 'Acknowledged', 'Investigating', 'Resolved'], default: 'Open' })
    status: 'Open' | 'Acknowledged' | 'Investigating' | 'Resolved';

    @Prop({ type: [Comment], default: [] })
    comments: Comment[];    



    createdAt: Date;
    updatedAt: Date;
}

export const FeedbackSchema = SchemaFactory.createForClass(Feedback);