package assignments

import (
	"boostai-backend/internal/aireview"
	"time"
)

type AssignmentResponse struct {
	ID            int64      `json:"id"`
	ClassroomID   int64      `json:"classroom_id"`
	TeacherID     int64      `json:"teacher_id"`
	Title         string     `json:"title"`
	Instructions  *string    `json:"instructions,omitempty"`
	PassThreshold *float64   `json:"pass_threshold,omitempty"`
	Status        string     `json:"status"`
	DueAt         *time.Time `json:"due_at,omitempty"`
	PublishedAt   *time.Time `json:"published_at,omitempty"`
	CreatedAt     *time.Time `json:"created_at,omitempty"`
	UpdatedAt     *time.Time `json:"updated_at,omitempty"`
}

type AssignmentQuestionResponse struct {
	AssignmentID      int64      `json:"assignment_id"`
	QuestionID        int64      `json:"question_id"`
	Position          int32      `json:"position"`
	AuthorTeacherID   int64      `json:"author_teacher_id"`
	Title             string     `json:"title"`
	Prompt            string     `json:"prompt"`
	Subject           *string    `json:"subject,omitempty"`
	Source            *string    `json:"source,omitempty"`
	QuestionStatus    string     `json:"question_status"`
	QuestionCreatedAt *time.Time `json:"question_created_at,omitempty"`
	QuestionUpdatedAt *time.Time `json:"question_updated_at,omitempty"`
}

type AssignmentStudentQuestionDetailResponse struct {
	AssignmentID              int64      `json:"assignment_id"`
	StudentID                 int64      `json:"student_id"`
	QuestionID                int64      `json:"question_id"`
	Position                  int32      `json:"position"`
	Title                     string     `json:"title"`
	Prompt                    string     `json:"prompt"`
	Subject                   *string    `json:"subject,omitempty"`
	Source                    *string    `json:"source,omitempty"`
	QuestionTags              []string   `json:"question_tags,omitempty"`
	QuestionStatus            string     `json:"question_status"`
	CorrectAnswer             *string    `json:"correct_answer,omitempty"`
	AssignmentAiFeedback      *string    `json:"assignment_ai_feedback,omitempty"`
	AssignmentTeacherFeedback *string    `json:"assignment_teacher_feedback,omitempty"`
	OverallScore              *float64   `json:"overall_score,omitempty"`
	PassThreshold             *float64   `json:"pass_threshold,omitempty"`
	NextStepOutcome           *string    `json:"next_step_outcome,omitempty"`
	PassStatusOverride        *string    `json:"pass_status_override,omitempty"`
	PassStatus                *string    `json:"pass_status,omitempty"`
	AnswerID                  *int64     `json:"answer_id,omitempty"`
	AnswerText                *string    `json:"answer_text,omitempty"`
	SolveMode                 *string    `json:"solve_mode,omitempty"`
	WorkingSteps              *string    `json:"working_steps,omitempty"`
	IsCorrect                 *bool      `json:"is_correct,omitempty"`
	AiFeedback                *string    `json:"ai_feedback,omitempty"`
	TeacherFeedback           *string    `json:"teacher_feedback,omitempty"`
	AnswerStatus              *string    `json:"answer_status,omitempty"`
	ReviewNeedsAttention      *bool      `json:"review_needs_attention,omitempty"`
	ReviewIssueReason         *string    `json:"review_issue_reason,omitempty"`
	ReviewCorrectnessScore    *float64   `json:"review_correctness_score,omitempty"`
	ReviewUnderstandingScore  *float64   `json:"review_understanding_score,omitempty"`
	ReviewQuestionScore       *float64   `json:"review_question_score,omitempty"`
	ReviewConfidence          *float64   `json:"review_confidence,omitempty"`
	ReviewTags                []string   `json:"review_tags,omitempty"`
	SubmittedAt               *time.Time `json:"submitted_at,omitempty"`
	ReviewedAt                *time.Time `json:"reviewed_at,omitempty"`
	AnswerCreatedAt           *time.Time `json:"answer_created_at,omitempty"`
	AnswerUpdatedAt           *time.Time `json:"answer_updated_at,omitempty"`
}

type updateAssignmentTeacherFeedbackRequest struct {
	TeacherFeedback    *string `json:"teacher_feedback"`
	PassStatusOverride *string `json:"pass_status_override"`
	NextStepOutcome    *string `json:"next_step_outcome"`
}

type updateAssignmentDraftRequest struct {
	ClassroomID   int64      `json:"classroom_id"`
	Title         string     `json:"title"`
	Instructions  *string    `json:"instructions"`
	PassThreshold *float64   `json:"pass_threshold"`
	DueAt         *time.Time `json:"due_at"`
}

type AssignmentReviewSummaryResponse struct {
	AssignmentID   int64 `json:"assignment_id"`
	TotalQuestions int64 `json:"total_questions"`
	TotalAssigned  int64 `json:"total_assigned"`
	NotStarted     int64 `json:"not_started"`
	InProgress     int64 `json:"in_progress"`
	Submitted      int64 `json:"submitted"`
	Reviewed       int64 `json:"reviewed"`
}

type AssignmentReviewQueueItemResponse struct {
	AssignmentID        int64      `json:"assignment_id"`
	StudentID           int64      `json:"student_id"`
	NextStepOutcome     *string    `json:"next_step_outcome,omitempty"`
	StudentName         string     `json:"student_name"`
	StudentEmail        string     `json:"student_email"`
	TotalQuestions      int64      `json:"total_questions"`
	AnsweredQuestions   int64      `json:"answered_questions"`
	ReviewedQuestions   int64      `json:"reviewed_questions"`
	SubmittedQuestions  int64      `json:"submitted_questions"`
	InProgressQuestions int64      `json:"in_progress_questions"`
	ReviewStatus        string     `json:"review_status"`
	LatestSubmittedAt   *time.Time `json:"latest_submitted_at,omitempty"`
	LatestReviewedAt    *time.Time `json:"latest_reviewed_at,omitempty"`
}

type assignmentCloseReadiness struct {
	CanClose bool
	Blockers []string
}

type StudentWeaknessSummaryResponse struct {
	StudentID    int64              `json:"student_id"`
	TopicScores  map[string]float64 `json:"topic_scores"`
	WeakTags     []string           `json:"weak_tags"`
	RecentIssues []string           `json:"recent_issues"`
}

type AssignmentRedoPlanResponse struct {
	AssignmentID        int64                          `json:"assignment_id"`
	StudentID           int64                          `json:"student_id"`
	RedoPlanGeneratedAt *time.Time                     `json:"redo_plan_generated_at,omitempty"`
	TeacherFeedback     *string                        `json:"teacher_feedback,omitempty"`
	WeaknessSummary     StudentWeaknessSummaryResponse `json:"weakness_summary"`
	Plan                *aireview.RedoPlanResult       `json:"plan,omitempty"`
	Error               string                         `json:"error,omitempty"`
}

type createAssignmentRequest struct {
	ClassroomID   int64      `json:"classroom_id"`
	TeacherID     int64      `json:"teacher_id"`
	Title         string     `json:"title"`
	Instructions  *string    `json:"instructions"`
	PassThreshold *float64   `json:"pass_threshold"`
	Status        string     `json:"status"`
	DueAt         *time.Time `json:"due_at"`
	PublishedAt   *time.Time `json:"published_at"`
}

type assignStudentToAssignmentRequest struct {
	StudentID       int64                                 `json:"student_id"`
	MixedGeneration *generateMixedStudentQuestionsRequest `json:"mixed_generation"`
}

type addQuestionToAssignmentRequest struct {
	QuestionID int64 `json:"question_id"`
	Position   int32 `json:"position"`
}

type generateMixedStudentQuestionsRequest struct {
	PrimaryTopic           string   `json:"primary_topic"`
	PrimaryDifficulty      string   `json:"primary_difficulty"`
	TotalQuestions         int      `json:"total_questions"`
	PersonalizedRatio      *float64 `json:"personalized_ratio"`
	Seed                   *int64   `json:"seed"`
	PersonalizedDifficulty *string  `json:"personalized_difficulty"`
	Subject                *string  `json:"subject"`
	QuestionStatus         *string  `json:"question_status"`
	QuestionSource         *string  `json:"question_source"`
}

type mixedPlanWeaknessSummaryResponse struct {
	TopicScores  map[string]float64 `json:"topic_scores"`
	WeakTags     []string           `json:"weak_tags"`
	RecentIssues []string           `json:"recent_issues"`
}

type mixedPlanQuestionResponse struct {
	MappingID         int64      `json:"mapping_id"`
	QuestionID        int64      `json:"question_id"`
	Position          int32      `json:"position"`
	SourceBucket      string     `json:"source_bucket"`
	SourceTopic       *string    `json:"source_topic,omitempty"`
	SourceDifficulty  *string    `json:"source_difficulty,omitempty"`
	GeneratorSeed     *int64     `json:"generator_seed,omitempty"`
	Title             string     `json:"title"`
	Prompt            string     `json:"prompt"`
	Subject           *string    `json:"subject,omitempty"`
	QuestionStatus    string     `json:"question_status"`
	QuestionSource    *string    `json:"question_source,omitempty"`
	CorrectAnswer     *string    `json:"correct_answer,omitempty"`
	Tags              []string   `json:"tags,omitempty"`
	QuestionCreatedAt *time.Time `json:"question_created_at,omitempty"`
	QuestionUpdatedAt *time.Time `json:"question_updated_at,omitempty"`
}

type generateMixedStudentQuestionsResponse struct {
	AssignmentID        int64                            `json:"assignment_id"`
	StudentID           int64                            `json:"student_id"`
	PrimaryTopic        string                           `json:"primary_topic"`
	PrimaryDifficulty   string                           `json:"primary_difficulty"`
	TotalQuestions      int                              `json:"total_questions"`
	CoreCount           int                              `json:"core_count"`
	PersonalizedCount   int                              `json:"personalized_count"`
	PersonalizedApplied bool                             `json:"personalized_applied"`
	PersonalizedTopic   *string                          `json:"personalized_topic,omitempty"`
	PersonalizedRatio   float64                          `json:"personalized_ratio"`
	BaseSeed            int64                            `json:"base_seed"`
	WeaknessSummary     mixedPlanWeaknessSummaryResponse `json:"weakness_summary"`
	Questions           []mixedPlanQuestionResponse      `json:"questions"`
}

type assignmentAPIError struct {
	status  int
	code    string
	message string
}

func (e *assignmentAPIError) Error() string {
	if e == nil {
		return ""
	}
	return e.message
}

type weaknessSummary struct {
	TopicScores  map[string]float64 `json:"topicScores"`
	WeakTags     []string           `json:"weakTags"`
	RecentIssues []string           `json:"recentIssues"`
}

type storedRedoPlan struct {
	TeacherFeedback string                   `json:"teacherFeedback,omitempty"`
	WeaknessSummary weaknessSummary          `json:"weaknessSummary"`
	Plan            *aireview.RedoPlanResult `json:"plan,omitempty"`
	Error           string                   `json:"error,omitempty"`
}
