package assignmentgen

import (
	"context"
	"fmt"

	"boostai-backend/internal/sqlc"
)

const defaultPersonalizedRatio = 0.30

type WeaknessSummary struct {
	TopicScores  map[sqlc.QuestionTopic]float64
	WeakTags     []string
	RecentIssues []string
}

type MixedPlanParams struct {
	StudentID              int64
	PrimaryTopic           sqlc.QuestionTopic
	PrimaryDifficulty      sqlc.QuestionDifficulty
	TotalQuestions         int
	PersonalizedRatio      float64
	BaseSeed               int64
	PersonalizedDifficulty sqlc.QuestionDifficulty
}

type MixedPlan struct {
	Plan                []PlanItem
	WeaknessSummary     WeaknessSummary
	CoreCount           int
	PersonalizedCount   int
	PersonalizedTopic   sqlc.QuestionTopic
	PersonalizedApplied bool
	BaseSeed            int64
}

type GenerateMixedStudentQuestionSetParams struct {
	AssignmentID           int64
	StudentID              int64
	TeacherID              int64
	Subject                string
	QuestionStatus         sqlc.QuestionStatus
	QuestionSource         string
	PrimaryTopic           sqlc.QuestionTopic
	PrimaryDifficulty      sqlc.QuestionDifficulty
	TotalQuestions         int
	PersonalizedRatio      float64
	Seed                   int64
	PersonalizedDifficulty sqlc.QuestionDifficulty
}

type GenerateMixedStudentQuestionSetResult struct {
	StoredQuestions []StoredStudentQuestion
	MixedPlan       MixedPlan
}

func (s *Service) BuildWeaknessSummary(ctx context.Context, studentID int64) (WeaknessSummary, error) {
	if s == nil || s.db == nil || s.db.Pool == nil {
		return WeaknessSummary{}, fmt.Errorf("assignment question generator database is not configured")
	}
	if studentID <= 0 {
		return WeaknessSummary{}, fmt.Errorf("student_id is required")
	}

	queries := sqlc.New(s.db.Pool)
	rows, err := queries.ListStudentPlanningPerformance(ctx, studentID)
	if err != nil {
		return WeaknessSummary{}, err
	}

	return buildWeaknessSummary(rows), nil
}

func (s *Service) GenerateAndStoreMixedStudentQuestions(ctx context.Context, params GenerateMixedStudentQuestionSetParams) (GenerateMixedStudentQuestionSetResult, error) {
	mixedPlan, err := s.BuildMixedPlan(ctx, MixedPlanParams{
		StudentID:              params.StudentID,
		PrimaryTopic:           params.PrimaryTopic,
		PrimaryDifficulty:      params.PrimaryDifficulty,
		TotalQuestions:         params.TotalQuestions,
		PersonalizedRatio:      params.PersonalizedRatio,
		BaseSeed:               params.Seed,
		PersonalizedDifficulty: params.PersonalizedDifficulty,
	})
	if err != nil {
		return GenerateMixedStudentQuestionSetResult{}, err
	}

	storedQuestions, err := s.GenerateAndStoreStudentQuestions(ctx, GenerateStudentQuestionSetParams{
		AssignmentID:   params.AssignmentID,
		StudentID:      params.StudentID,
		TeacherID:      params.TeacherID,
		Subject:        params.Subject,
		QuestionStatus: params.QuestionStatus,
		QuestionSource: params.QuestionSource,
		Plan:           mixedPlan.Plan,
	})
	if err != nil {
		return GenerateMixedStudentQuestionSetResult{}, err
	}

	return GenerateMixedStudentQuestionSetResult{
		StoredQuestions: storedQuestions,
		MixedPlan:       mixedPlan,
	}, nil
}
