package repository

import (
	dbgen "Yimaru-Backend/gen/db"
	"Yimaru-Backend/internal/domain"
	"context"

	"github.com/jackc/pgx/v5/pgtype"
)

func (s *Store) CreateSubCourse(
	ctx context.Context,
	courseID int64,
	title string,
	description *string,
	thumbnail *string,
	displayOrder *int32,
	level string,
) (domain.SubCourse, error) {
	var descText, thumbText pgtype.Text
	if description != nil {
		descText = pgtype.Text{String: *description, Valid: true}
	}
	if thumbnail != nil {
		thumbText = pgtype.Text{String: *thumbnail, Valid: true}
	}

	var dispOrder pgtype.Int4
	if displayOrder != nil {
		dispOrder = pgtype.Int4{Int32: *displayOrder, Valid: true}
	}

	row, err := s.queries.CreateSubCourse(ctx, dbgen.CreateSubCourseParams{
		CourseID:    courseID,
		Title:       title,
		Description: descText,
		Thumbnail:   thumbText,
		Column5:     dispOrder,
		Level:       level,
		Column7:     pgtype.Bool{Bool: true, Valid: true},
	})
	if err != nil {
		return domain.SubCourse{}, err
	}

	return domain.SubCourse{
		ID:           row.ID,
		CourseID:     row.CourseID,
		Title:        row.Title,
		Description:  ptrString(row.Description),
		Thumbnail:    ptrString(row.Thumbnail),
		DisplayOrder: row.DisplayOrder,
		Level:        row.Level,
		IsActive:     row.IsActive,
	}, nil
}

func (s *Store) GetSubCourseByID(
	ctx context.Context,
	id int64,
) (domain.SubCourse, error) {
	row, err := s.queries.GetSubCourseByID(ctx, id)
	if err != nil {
		return domain.SubCourse{}, err
	}

	return domain.SubCourse{
		ID:           row.ID,
		CourseID:     row.CourseID,
		Title:        row.Title,
		Description:  ptrString(row.Description),
		Thumbnail:    ptrString(row.Thumbnail),
		DisplayOrder: row.DisplayOrder,
		Level:        row.Level,
		IsActive:     row.IsActive,
	}, nil
}

func (s *Store) GetSubCoursesByCourse(
	ctx context.Context,
	courseID int64,
) ([]domain.SubCourse, int64, error) {
	rows, err := s.queries.GetSubCoursesByCourse(ctx, courseID)
	if err != nil {
		return nil, 0, err
	}

	var (
		subCourses []domain.SubCourse
		totalCount int64
	)

	for i, row := range rows {
		if i == 0 {
			totalCount = row.TotalCount
		}

		subCourses = append(subCourses, domain.SubCourse{
			ID:           row.ID,
			CourseID:     row.CourseID,
			Title:        row.Title,
			Description:  ptrString(row.Description),
			Thumbnail:    ptrString(row.Thumbnail),
			DisplayOrder: row.DisplayOrder,
			Level:        row.Level,
			IsActive:     row.IsActive,
		})
	}

	return subCourses, totalCount, nil
}

func (s *Store) ListSubCoursesByCourse(
	ctx context.Context,
	courseID int64,
) ([]domain.SubCourse, error) {
	rows, err := s.queries.ListSubCoursesByCourse(ctx, courseID)
	if err != nil {
		return nil, err
	}

	subCourses := make([]domain.SubCourse, 0, len(rows))
	for _, row := range rows {
		subCourses = append(subCourses, domain.SubCourse{
			ID:           row.ID,
			CourseID:     row.CourseID,
			Title:        row.Title,
			Description:  ptrString(row.Description),
			Thumbnail:    ptrString(row.Thumbnail),
			DisplayOrder: row.DisplayOrder,
			Level:        row.Level,
			IsActive:     row.IsActive,
		})
	}

	return subCourses, nil
}

func (s *Store) ListActiveSubCourses(
	ctx context.Context,
) ([]domain.SubCourse, error) {
	rows, err := s.queries.ListActiveSubCourses(ctx)
	if err != nil {
		return nil, err
	}

	subCourses := make([]domain.SubCourse, 0, len(rows))
	for _, row := range rows {
		subCourses = append(subCourses, domain.SubCourse{
			ID:           row.ID,
			CourseID:     row.CourseID,
			Title:        row.Title,
			Description:  ptrString(row.Description),
			Thumbnail:    ptrString(row.Thumbnail),
			DisplayOrder: row.DisplayOrder,
			Level:        row.Level,
			IsActive:     row.IsActive,
		})
	}

	return subCourses, nil
}

func (s *Store) UpdateSubCourse(
	ctx context.Context,
	id int64,
	title *string,
	description *string,
	thumbnail *string,
	displayOrder *int32,
	level *string,
	isActive *bool,
) error {
	var titleVal, descVal, thumbVal, levelVal string
	var dispOrderVal int32
	var isActiveVal bool

	if title != nil {
		titleVal = *title
	}
	if description != nil {
		descVal = *description
	}
	if thumbnail != nil {
		thumbVal = *thumbnail
	}
	if displayOrder != nil {
		dispOrderVal = *displayOrder
	}
	if level != nil {
		levelVal = *level
	}
	if isActive != nil {
		isActiveVal = *isActive
	}

	return s.queries.UpdateSubCourse(ctx, dbgen.UpdateSubCourseParams{
		Title:        titleVal,
		Description:  pgtype.Text{String: descVal, Valid: description != nil},
		Thumbnail:    pgtype.Text{String: thumbVal, Valid: thumbnail != nil},
		DisplayOrder: dispOrderVal,
		Level:        levelVal,
		IsActive:     isActiveVal,
		ID:           id,
	})
}

func (s *Store) DeactivateSubCourse(
	ctx context.Context,
	id int64,
) error {
	return s.queries.DeactivateSubCourse(ctx, id)
}

func (s *Store) DeleteSubCourse(
	ctx context.Context,
	id int64,
) (domain.SubCourse, error) {
	row, err := s.queries.DeleteSubCourse(ctx, id)
	if err != nil {
		return domain.SubCourse{}, err
	}

	return domain.SubCourse{
		ID:           row.ID,
		CourseID:     row.CourseID,
		Title:        row.Title,
		Description:  ptrString(row.Description),
		Thumbnail:    ptrString(row.Thumbnail),
		DisplayOrder: row.DisplayOrder,
		Level:        row.Level,
		IsActive:     row.IsActive,
	}, nil
}


