package services_test

import (
	"regexp"
	"testing"
	"time"

	"github.com/DATA-DOG/go-sqlmock"
	"github.com/rede5/gohorsejobs/backend/internal/dto"
	"github.com/rede5/gohorsejobs/backend/internal/services"
	"github.com/stretchr/testify/assert"
)

func TestCreateJob(t *testing.T) {
	db, mock, err := sqlmock.New()
	if err != nil {
		t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
	}
	defer db.Close()

	service := services.NewJobService(db)

	tests := []struct {
		name    string
		req     dto.CreateJobRequest
		mockRun func()
		wantErr bool
	}{
		{
			name: "Success",
			req: dto.CreateJobRequest{
				CompanyID: "1",
				Title:     "Go Developer",
				Status:    "published",
			},
			mockRun: func() {
				mock.ExpectQuery(regexp.QuoteMeta(`INSERT INTO jobs`)).
					WithArgs("1", "Go Developer", sqlmock.AnyArg(), sqlmock.AnyArg(), sqlmock.AnyArg(), sqlmock.AnyArg(), sqlmock.AnyArg(), sqlmock.AnyArg(), sqlmock.AnyArg(), sqlmock.AnyArg(), sqlmock.AnyArg(), sqlmock.AnyArg(), sqlmock.AnyArg(), sqlmock.AnyArg(), sqlmock.AnyArg(), "published", sqlmock.AnyArg(), sqlmock.AnyArg()).
					WillReturnRows(sqlmock.NewRows([]string{"id", "created_at", "updated_at"}).AddRow("100", time.Now(), time.Now()))
			},
			wantErr: false,
		},
		{
			name: "DB Error",
			req: dto.CreateJobRequest{
				CompanyID: "1",
				Title:     "Go Developer",
			},
			mockRun: func() {
				mock.ExpectQuery(regexp.QuoteMeta(`INSERT INTO jobs`)).
					WillReturnError(assert.AnError)
			},
			wantErr: true,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			tt.mockRun()
			got, err := service.CreateJob(tt.req)
			if (err != nil) != tt.wantErr {
				t.Errorf("JobService.CreateJob() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
			if !tt.wantErr {
				assert.Equal(t, "100", got.ID)
			}
		})
	}
}

func TestGetJobs(t *testing.T) {
	db, mock, err := sqlmock.New()
	if err != nil {
		t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
	}
	defer db.Close()

	service := services.NewJobService(db)

	tests := []struct {
		name    string
		filter  dto.JobFilterQuery
		mockRun func()
		wantErr bool
	}{
		{
			name: "List All",
			filter: dto.JobFilterQuery{
				PaginationQuery: dto.PaginationQuery{Page: 1, Limit: 10},
			},
			mockRun: func() {
				// List query
				mock.ExpectQuery(regexp.QuoteMeta(`SELECT
                        j.id, j.company_id, j.title, j.description, j.salary_min, j.salary_max, j.salary_type,
                        j.employment_type, j.work_mode, j.location, j.status, j.is_featured, j.created_at, j.updated_at,
                        c.name as company_name, c.logo_url as company_logo_url,
                        r.name as region_name, ci.name as city_name
                FROM jobs j`)).
					WillReturnRows(sqlmock.NewRows([]string{
						"id", "company_id", "title", "description", "salary_min", "salary_max", "salary_type",
						"employment_type", "work_mode", "location", "status", "is_featured", "created_at", "updated_at",
						"company_name", "company_logo_url", "region_name", "city_name",
					}).AddRow(
						"1", "10", "Dev", "Desc", 100, 200, "m", "ft", "Remote", "Remote", "open", false, time.Now(), time.Now(),
						"Acme", "url", "Region", "City",
					))

				// Count query
				mock.ExpectQuery(regexp.QuoteMeta(`SELECT COUNT(*) FROM jobs j WHERE 1=1`)).
					WillReturnRows(sqlmock.NewRows([]string{"count"}).AddRow(2))
			},
			wantErr: false,
		},
	}

	for _, tt := range tests {
		t.Run(tt.name, func(t *testing.T) {
			tt.mockRun()
			_, _, err := service.GetJobs(tt.filter)
			if (err != nil) != tt.wantErr {
				t.Errorf("JobService.GetJobs() error = %v, wantErr %v", err, tt.wantErr)
				return
			}
		})
	}
}
