import mongoose, { Schema, Document } from 'mongoose';

export interface ITaskCompletion extends Document {
  date: string;              // "2024-01-15" format
  blockType: string;         // matches schedule block type
  activityName: string;
  scheduledStart: string;    // "08:30"
  scheduledEnd: string;      // "16:00"
  completed: boolean | null; // null = not yet asked
  completedAt?: Date;      // timestamp when marked
  note?: string;             // optional user note
}

const TaskCompletionSchema: Schema = new Schema({
  date: { type: String, required: true },
  blockType: { type: String, required: true },
  activityName: { type: String, required: true },
  scheduledStart: { type: String, required: true },
  scheduledEnd: { type: String, required: true },
  completed: { type: Boolean, default: null },
  completedAt: { type: Date },
  note: { type: String },
});

// Ensure uniqueness per block to avoid duplicates
TaskCompletionSchema.index({ date: 1, scheduledStart: 1, activityName: 1 }, { unique: true });

export default mongoose.models.TaskCompletion || mongoose.model<ITaskCompletion>('TaskCompletion', TaskCompletionSchema);
