package sql_test

import (
	"context"
	"database/sql"
	"regexp"
	"testing"
	repo "tercul/internal/data/sql"
	"tercul/internal/domain"
	"tercul/internal/platform/config"
	"time"

	"github.com/DATA-DOG/go-sqlmock"
	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestNewCommentRepository(t *testing.T) {
	db, _, err := newMockDb()
	require.NoError(t, err)
	cfg, err := config.LoadConfig()
	require.NoError(t, err)
	repo := repo.NewCommentRepository(db, cfg)
	assert.NotNil(t, repo)
}

func TestCommentRepository_ListByUserID(t *testing.T) {
	t.Run("should return comments for a given user id", func(t *testing.T) {
		db, mock, err := newMockDb()
		require.NoError(t, err)
		cfg, err := config.LoadConfig()
		require.NoError(t, err)
		repo := repo.NewCommentRepository(db, cfg)

		userID := uint(1)
		expectedComments := []domain.Comment{
			{BaseModel: domain.BaseModel{ID: 1, CreatedAt: time.Now(), UpdatedAt: time.Now()}},
			{BaseModel: domain.BaseModel{ID: 2, CreatedAt: time.Now(), UpdatedAt: time.Now()}},
		}

		rows := sqlmock.NewRows([]string{"id", "created_at", "updated_at"}).
			AddRow(expectedComments[0].ID, expectedComments[0].CreatedAt, expectedComments[0].UpdatedAt).
			AddRow(expectedComments[1].ID, expectedComments[1].CreatedAt, expectedComments[1].UpdatedAt)

		mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "comments" WHERE user_id = $1`)).
			WithArgs(userID).
			WillReturnRows(rows)

		comments, err := repo.ListByUserID(context.Background(), userID)
		require.NoError(t, err)
		assert.Equal(t, expectedComments, comments)
		assert.NoError(t, mock.ExpectationsWereMet())
	})

	t.Run("should return error if query fails", func(t *testing.T) {
		db, mock, err := newMockDb()
		require.NoError(t, err)
		cfg, err := config.LoadConfig()
		require.NoError(t, err)
		repo := repo.NewCommentRepository(db, cfg)

		userID := uint(1)

		mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "comments" WHERE user_id = $1`)).
			WithArgs(userID).
			WillReturnError(sql.ErrNoRows)

		comments, err := repo.ListByUserID(context.Background(), userID)
		require.Error(t, err)
		assert.Nil(t, comments)
		assert.NoError(t, mock.ExpectationsWereMet())
	})
}

func TestCommentRepository_ListByWorkID(t *testing.T) {
	t.Run("should return comments for a given work id", func(t *testing.T) {
		db, mock, err := newMockDb()
		require.NoError(t, err)
		cfg, err := config.LoadConfig()
		require.NoError(t, err)
		repo := repo.NewCommentRepository(db, cfg)

		workID := uint(1)
		expectedComments := []domain.Comment{
			{BaseModel: domain.BaseModel{ID: 1, CreatedAt: time.Now(), UpdatedAt: time.Now()}},
			{BaseModel: domain.BaseModel{ID: 2, CreatedAt: time.Now(), UpdatedAt: time.Now()}},
		}

		rows := sqlmock.NewRows([]string{"id", "created_at", "updated_at"}).
			AddRow(expectedComments[0].ID, expectedComments[0].CreatedAt, expectedComments[0].UpdatedAt).
			AddRow(expectedComments[1].ID, expectedComments[1].CreatedAt, expectedComments[1].UpdatedAt)

		mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "comments" WHERE work_id = $1`)).
			WithArgs(workID).
			WillReturnRows(rows)

		comments, err := repo.ListByWorkID(context.Background(), workID)
		require.NoError(t, err)
		assert.Equal(t, expectedComments, comments)
		assert.NoError(t, mock.ExpectationsWereMet())
	})

	t.Run("should return error if query fails", func(t *testing.T) {
		db, mock, err := newMockDb()
		require.NoError(t, err)
		cfg, err := config.LoadConfig()
		require.NoError(t, err)
		repo := repo.NewCommentRepository(db, cfg)

		workID := uint(1)

		mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "comments" WHERE work_id = $1`)).
			WithArgs(workID).
			WillReturnError(sql.ErrNoRows)

		comments, err := repo.ListByWorkID(context.Background(), workID)
		require.Error(t, err)
		assert.Nil(t, comments)
		assert.NoError(t, mock.ExpectationsWereMet())
	})
}

func TestCommentRepository_ListByTranslationID(t *testing.T) {
	t.Run("should return comments for a given translation id", func(t *testing.T) {
		db, mock, err := newMockDb()
		require.NoError(t, err)
		cfg, err := config.LoadConfig()
		require.NoError(t, err)
		repo := repo.NewCommentRepository(db, cfg)

		translationID := uint(1)
		expectedComments := []domain.Comment{
			{BaseModel: domain.BaseModel{ID: 1, CreatedAt: time.Now(), UpdatedAt: time.Now()}},
			{BaseModel: domain.BaseModel{ID: 2, CreatedAt: time.Now(), UpdatedAt: time.Now()}},
		}

		rows := sqlmock.NewRows([]string{"id", "created_at", "updated_at"}).
			AddRow(expectedComments[0].ID, expectedComments[0].CreatedAt, expectedComments[0].UpdatedAt).
			AddRow(expectedComments[1].ID, expectedComments[1].CreatedAt, expectedComments[1].UpdatedAt)

		mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "comments" WHERE translation_id = $1`)).
			WithArgs(translationID).
			WillReturnRows(rows)

		comments, err := repo.ListByTranslationID(context.Background(), translationID)
		require.NoError(t, err)
		assert.Equal(t, expectedComments, comments)
		assert.NoError(t, mock.ExpectationsWereMet())
	})

	t.Run("should return error if query fails", func(t *testing.T) {
		db, mock, err := newMockDb()
		require.NoError(t, err)
		cfg, err := config.LoadConfig()
		require.NoError(t, err)
		repo := repo.NewCommentRepository(db, cfg)

		translationID := uint(1)

		mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "comments" WHERE translation_id = $1`)).
			WithArgs(translationID).
			WillReturnError(sql.ErrNoRows)

		comments, err := repo.ListByTranslationID(context.Background(), translationID)
		require.Error(t, err)
		assert.Nil(t, comments)
		assert.NoError(t, mock.ExpectationsWereMet())
	})
}

func TestCommentRepository_ListByParentID(t *testing.T) {
	t.Run("should return comments for a given parent id", func(t *testing.T) {
		db, mock, err := newMockDb()
		require.NoError(t, err)
		cfg, err := config.LoadConfig()
		require.NoError(t, err)
		repo := repo.NewCommentRepository(db, cfg)

		parentID := uint(1)
		expectedComments := []domain.Comment{
			{BaseModel: domain.BaseModel{ID: 1, CreatedAt: time.Now(), UpdatedAt: time.Now()}},
			{BaseModel: domain.BaseModel{ID: 2, CreatedAt: time.Now(), UpdatedAt: time.Now()}},
		}

		rows := sqlmock.NewRows([]string{"id", "created_at", "updated_at"}).
			AddRow(expectedComments[0].ID, expectedComments[0].CreatedAt, expectedComments[0].UpdatedAt).
			AddRow(expectedComments[1].ID, expectedComments[1].CreatedAt, expectedComments[1].UpdatedAt)

		mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "comments" WHERE parent_id = $1`)).
			WithArgs(parentID).
			WillReturnRows(rows)

		comments, err := repo.ListByParentID(context.Background(), parentID)
		require.NoError(t, err)
		assert.Equal(t, expectedComments, comments)
		assert.NoError(t, mock.ExpectationsWereMet())
	})

	t.Run("should return error if query fails", func(t *testing.T) {
		db, mock, err := newMockDb()
		require.NoError(t, err)
		cfg, err := config.LoadConfig()
		require.NoError(t, err)
		repo := repo.NewCommentRepository(db, cfg)

		parentID := uint(1)

		mock.ExpectQuery(regexp.QuoteMeta(`SELECT * FROM "comments" WHERE parent_id = $1`)).
			WithArgs(parentID).
			WillReturnError(sql.ErrNoRows)

		comments, err := repo.ListByParentID(context.Background(), parentID)
		require.Error(t, err)
		assert.Nil(t, comments)
		assert.NoError(t, mock.ExpectationsWereMet())
	})
}