package handlers

import (
	"net/http"

	"github.com/breakpilot/school-service/internal/models"
	"github.com/gin-gonic/gin"
)

// School Year Handlers

// CreateSchoolYear creates a new school year
func (h *Handler) CreateSchoolYear(c *gin.Context) {
	teacherID := getUserID(c)
	if teacherID == "" {
		respondError(c, http.StatusUnauthorized, "User not authenticated")
		return
	}

	var req models.CreateSchoolYearRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		respondError(c, http.StatusBadRequest, "Invalid request: "+err.Error())
		return
	}

	year, err := h.classService.CreateSchoolYear(c.Request.Context(), teacherID, &req)
	if err != nil {
		respondError(c, http.StatusInternalServerError, "Failed to create school year: "+err.Error())
		return
	}

	respondCreated(c, year)
}

// GetSchoolYears returns all school years for the current teacher
func (h *Handler) GetSchoolYears(c *gin.Context) {
	teacherID := getUserID(c)
	if teacherID == "" {
		respondError(c, http.StatusUnauthorized, "User not authenticated")
		return
	}

	years, err := h.classService.GetSchoolYears(c.Request.Context(), teacherID)
	if err != nil {
		respondError(c, http.StatusInternalServerError, "Failed to get school years: "+err.Error())
		return
	}

	respondSuccess(c, years)
}

// Class Handlers

// CreateClass creates a new class
func (h *Handler) CreateClass(c *gin.Context) {
	teacherID := getUserID(c)
	if teacherID == "" {
		respondError(c, http.StatusUnauthorized, "User not authenticated")
		return
	}

	var req models.CreateClassRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		respondError(c, http.StatusBadRequest, "Invalid request: "+err.Error())
		return
	}

	class, err := h.classService.CreateClass(c.Request.Context(), teacherID, &req)
	if err != nil {
		respondError(c, http.StatusInternalServerError, "Failed to create class: "+err.Error())
		return
	}

	respondCreated(c, class)
}

// GetClasses returns all classes for the current teacher
func (h *Handler) GetClasses(c *gin.Context) {
	teacherID := getUserID(c)
	if teacherID == "" {
		respondError(c, http.StatusUnauthorized, "User not authenticated")
		return
	}

	classes, err := h.classService.GetClasses(c.Request.Context(), teacherID)
	if err != nil {
		respondError(c, http.StatusInternalServerError, "Failed to get classes: "+err.Error())
		return
	}

	respondSuccess(c, classes)
}

// GetClass returns a single class
func (h *Handler) GetClass(c *gin.Context) {
	teacherID := getUserID(c)
	classID := c.Param("id")

	if teacherID == "" {
		respondError(c, http.StatusUnauthorized, "User not authenticated")
		return
	}

	class, err := h.classService.GetClass(c.Request.Context(), classID, teacherID)
	if err != nil {
		respondError(c, http.StatusNotFound, "Class not found")
		return
	}

	respondSuccess(c, class)
}

// DeleteClass deletes a class
func (h *Handler) DeleteClass(c *gin.Context) {
	teacherID := getUserID(c)
	classID := c.Param("id")

	if teacherID == "" {
		respondError(c, http.StatusUnauthorized, "User not authenticated")
		return
	}

	if err := h.classService.DeleteClass(c.Request.Context(), classID, teacherID); err != nil {
		respondError(c, http.StatusInternalServerError, "Failed to delete class: "+err.Error())
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "Class deleted"})
}

// Student Handlers

// CreateStudent creates a new student in a class
func (h *Handler) CreateStudent(c *gin.Context) {
	classID := c.Param("id")

	var req models.CreateStudentRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		respondError(c, http.StatusBadRequest, "Invalid request: "+err.Error())
		return
	}

	student, err := h.classService.CreateStudent(c.Request.Context(), classID, &req)
	if err != nil {
		respondError(c, http.StatusInternalServerError, "Failed to create student: "+err.Error())
		return
	}

	respondCreated(c, student)
}

// GetStudents returns all students in a class
func (h *Handler) GetStudents(c *gin.Context) {
	classID := c.Param("id")

	students, err := h.classService.GetStudents(c.Request.Context(), classID)
	if err != nil {
		respondError(c, http.StatusInternalServerError, "Failed to get students: "+err.Error())
		return
	}

	respondSuccess(c, students)
}

// DeleteStudent deletes a student
func (h *Handler) DeleteStudent(c *gin.Context) {
	studentID := c.Param("studentId")

	if err := h.classService.DeleteStudent(c.Request.Context(), studentID); err != nil {
		respondError(c, http.StatusInternalServerError, "Failed to delete student: "+err.Error())
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "Student deleted"})
}

// ImportStudents imports students from CSV (placeholder)
func (h *Handler) ImportStudents(c *gin.Context) {
	// TODO: Implement CSV import
	// For now, return a not implemented response
	respondError(c, http.StatusNotImplemented, "CSV import not yet implemented")
}

// Subject Handlers

// CreateSubject creates a new subject
func (h *Handler) CreateSubject(c *gin.Context) {
	teacherID := getUserID(c)
	if teacherID == "" {
		respondError(c, http.StatusUnauthorized, "User not authenticated")
		return
	}

	var req models.CreateSubjectRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		respondError(c, http.StatusBadRequest, "Invalid request: "+err.Error())
		return
	}

	subject, err := h.classService.CreateSubject(c.Request.Context(), teacherID, &req)
	if err != nil {
		respondError(c, http.StatusInternalServerError, "Failed to create subject: "+err.Error())
		return
	}

	respondCreated(c, subject)
}

// GetSubjects returns all subjects for the current teacher
func (h *Handler) GetSubjects(c *gin.Context) {
	teacherID := getUserID(c)
	if teacherID == "" {
		respondError(c, http.StatusUnauthorized, "User not authenticated")
		return
	}

	subjects, err := h.classService.GetSubjects(c.Request.Context(), teacherID)
	if err != nil {
		respondError(c, http.StatusInternalServerError, "Failed to get subjects: "+err.Error())
		return
	}

	respondSuccess(c, subjects)
}

// DeleteSubject deletes a subject
func (h *Handler) DeleteSubject(c *gin.Context) {
	teacherID := getUserID(c)
	subjectID := c.Param("id")

	if teacherID == "" {
		respondError(c, http.StatusUnauthorized, "User not authenticated")
		return
	}

	if err := h.classService.DeleteSubject(c.Request.Context(), subjectID, teacherID); err != nil {
		respondError(c, http.StatusInternalServerError, "Failed to delete subject: "+err.Error())
		return
	}

	c.JSON(http.StatusOK, gin.H{"message": "Subject deleted"})
}
