package repository

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

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

func (s *Store) CreateCourse(
	ctx context.Context,
	categoryID int64,
	title string,
	description *string,
	thumbnail *string,
) (domain.Course, error) {
	var descVal, thumbVal string
	if description != nil {
		descVal = *description
	}
	if thumbnail != nil {
		thumbVal = *thumbnail
	}

	row, err := s.queries.CreateCourse(ctx, dbgen.CreateCourseParams{
		CategoryID:  categoryID,
		Title:       title,
		Description: pgtype.Text{String: descVal, Valid: description != nil},
		Thumbnail:   pgtype.Text{String: thumbVal, Valid: thumbnail != nil},
		Column5:     true,
	})
	if err != nil {
		return domain.Course{}, err
	}

	return domain.Course{
		ID:          row.ID,
		CategoryID:  row.CategoryID,
		Title:       row.Title,
		Description: &row.Description.String,
		Thumbnail:   &row.Thumbnail.String,
		IsActive:    row.IsActive,
	}, nil
}

func (s *Store) GetCourseByID(
	ctx context.Context,
	id int64,
) (domain.Course, error) {

	row, err := s.queries.GetCourseByID(ctx, id)
	if err != nil {
		return domain.Course{}, err
	}

	return domain.Course{
		ID:          row.ID,
		CategoryID:  row.CategoryID,
		Title:       row.Title,
		Description: &row.Description.String,
		Thumbnail:   &row.Thumbnail.String,
		IsActive:    row.IsActive,
	}, nil
}

func (s *Store) GetCoursesByCategory(
	ctx context.Context,
	categoryID int64,
	limit int32,
	offset int32,
) ([]domain.Course, int64, error) {

	rows, err := s.queries.GetCoursesByCategory(ctx, dbgen.GetCoursesByCategoryParams{
		CategoryID: categoryID,
		Limit:      pgtype.Int4{Int32: limit},
		Offset:     pgtype.Int4{Int32: offset},
	})
	if err != nil {
		return nil, 0, err
	}

	var (
		courses    []domain.Course
		totalCount int64
	)

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

		courses = append(courses, domain.Course{
			ID:          row.ID,
			CategoryID:  row.CategoryID,
			Title:       row.Title,
			Description: &row.Description.String,
			Thumbnail:   &row.Thumbnail.String,
			IsActive:    row.IsActive,
		})
	}

	return courses, totalCount, nil
}

func (s *Store) UpdateCourse(
	ctx context.Context,
	id int64,
	title *string,
	description *string,
	thumbnail *string,
	isActive *bool,
) error {
	var (
		titleVal       string
		descriptionVal string
		thumbnailVal   string
		isActiveVal    bool
	)

	if title != nil {
		titleVal = *title
	}
	if description != nil {
		descriptionVal = *description
	}
	if thumbnail != nil {
		thumbnailVal = *thumbnail
	}
	if isActive != nil {
		isActiveVal = *isActive
	}

	return s.queries.UpdateCourse(ctx, dbgen.UpdateCourseParams{
		Title:       titleVal,
		Description: pgtype.Text{String: descriptionVal, Valid: description != nil},
		Thumbnail:   pgtype.Text{String: thumbnailVal, Valid: thumbnail != nil},
		IsActive:    isActiveVal,
		ID:          id,
	})
}

func (s *Store) DeleteCourse(
	ctx context.Context,
	id int64,
) error {

	return s.queries.DeleteCourse(ctx, id)
}
