package repo

import (
	"bytes"
	"database/sql"
	"fmt"
	"strconv"
	"strings"

	"github.com/jobs-scraper/internal/pkg/domain"
)

type JobRepository struct {
	db *sql.DB
}

func NewJobRepository(db *sql.DB) *JobRepository {
	return &JobRepository{db: db}
}

// JobExistsByLink checks if a job with the given link already exists in the database
func (r *JobRepository) JobExistsByLink(jobLink string) (bool, error) {
	var exists bool
	err := r.db.QueryRow("SELECT EXISTS(SELECT 1 FROM jobs WHERE job_link = $1)", jobLink).Scan(&exists)
	if err != nil {
		return false, fmt.Errorf("error checking job existence: %v", err)
	}
	return exists, nil
}

// SaveJob saves a single job and returns the generated ID
func (r *JobRepository) SaveJob(job domain.Job) (int64, error) {
	sqlStatement := `
		INSERT INTO jobs (title, company, company_link, location, job_link, job_timestamp, provider)
		VALUES ($1, $2, $3, $4, $5, $6, $7)
		ON CONFLICT (job_link) DO UPDATE SET
		title = EXCLUDED.title,
		company = EXCLUDED.company,
		company_link = EXCLUDED.company_link,
		location = EXCLUDED.location,
		job_timestamp = EXCLUDED.job_timestamp,
		provider = EXCLUDED.provider
		RETURNING id
	`

	var id int64
	err := r.db.QueryRow(sqlStatement,
		job.Title, job.Company, job.CompanyLink, job.Location,
		job.JobLink, job.JobPostTime, job.Provider,
	).Scan(&id)
	if err != nil {
		return 0, fmt.Errorf("error inserting job: %v", err)
	}

	return id, nil
}

func (r *JobRepository) SaveJobs(jobs []domain.Job) error {
	if len(jobs) == 0 {
		return nil
	}

	// Deduplicate jobs by job_link to avoid duplicate errors
	jobMap := make(map[string]domain.Job)
	for _, job := range jobs {
		jobMap[job.JobLink] = job
	}

	// Convert to slice of pointers for the bulk function
	uniqueJobs := make([]*domain.Job, 0, len(jobMap))
	for _, job := range jobMap {
		jobCopy := job // Important: create a copy to avoid pointer issues
		uniqueJobs = append(uniqueJobs, &jobCopy)
	}

	sqlTemplate := `
        INSERT INTO jobs (title, company, company_link, location, job_link, job_timestamp, provider)
        VALUES %s
        ON CONFLICT (job_link) DO UPDATE SET
        title = EXCLUDED.title,
        company = EXCLUDED.company,
        company_link = EXCLUDED.company_link,
        location = EXCLUDED.location,
        job_timestamp = EXCLUDED.job_timestamp,
        provider = EXCLUDED.provider
    `

	sqlStatement, vals := prepareQueryCreateBulk(sqlTemplate, uniqueJobs)

	_, err := r.db.Exec(sqlStatement, vals...)
	if err != nil {
		return fmt.Errorf("error inserting jobs: %v", err)
	}

	return nil
}

type JobFilter struct {
	Location   string
	Keywords   string
	Provider   domain.JobProvider
	PageSize   int
	PageNumber int
}

func (r *JobRepository) GetAllJobs(filter JobFilter) ([]domain.Job, int, error) {
	query := "SELECT id, title, company, company_link, location, job_link, job_timestamp FROM jobs"
	countQuery := "SELECT COUNT(*) FROM jobs"
	var conditions []string
	var args []interface{}
	paramIdx := 1

	if filter.Location != "" {
		conditions = append(conditions, fmt.Sprintf("location ILIKE $%d", paramIdx))
		args = append(args, "%"+filter.Location+"%")
		paramIdx++
	}

	if filter.Keywords != "" {
		conditions = append(conditions, fmt.Sprintf("(title ILIKE $%d OR company ILIKE $%d)", paramIdx, paramIdx))
		args = append(args, "%"+filter.Keywords+"%")
		paramIdx++
	}

	if filter.Provider > 0 {
		conditions = append(conditions, fmt.Sprintf("provider = $%d", paramIdx))
		args = append(args, filter.Provider)
		paramIdx++
	}

	whereClause := ""
	if len(conditions) > 0 {
		whereClause = " WHERE " + strings.Join(conditions, " AND ")
		query += whereClause
		countQuery += whereClause
	}

	var totalCount int
	countArgs := make([]interface{}, len(args))
	copy(countArgs, args)
	if err := r.db.QueryRow(countQuery, countArgs...).Scan(&totalCount); err != nil {
		return nil, 0, fmt.Errorf("error counting jobs: %v", err)
	}

	if filter.PageSize > 0 {
		query += fmt.Sprintf(" LIMIT $%d", paramIdx)
		args = append(args, filter.PageSize)
		paramIdx++

		if filter.PageNumber > 0 {
			offset := (filter.PageNumber - 1) * filter.PageSize
			query += fmt.Sprintf(" OFFSET $%d", paramIdx)
			args = append(args, offset)
			paramIdx++
		}
	}

	rows, err := r.db.Query(query, args...)
	if err != nil {
		return nil, 0, fmt.Errorf("error querying jobs: %v", err)
	}
	defer rows.Close()

	var jobs []domain.Job
	for rows.Next() {
		var job domain.Job
		if err := rows.Scan(&job.ID, &job.Title, &job.Company, &job.CompanyLink, &job.Location, &job.JobLink, &job.JobPostTime); err != nil {
			return nil, 0, fmt.Errorf("error scanning job row: %v", err)
		}
		jobs = append(jobs, job)
	}

	if err := rows.Err(); err != nil {
		return nil, 0, fmt.Errorf("error iterating over job rows: %v", err)
	}

	return jobs, totalCount, nil
}
func (r *JobRepository) GetJobByID(id int) (*domain.Job, error) {
	var job domain.Job

	sqlStatement := `
		SELECT id, title, company, company_link, location, job_link, job_timestamp 
		FROM jobs 
		WHERE id = $1
	`

	err := r.db.QueryRow(sqlStatement, id).Scan(
		&job.ID,
		&job.Title,
		&job.Company,
		&job.CompanyLink,
		&job.Location,
		&job.JobLink,
		&job.JobPostTime,
	)

	if err == sql.ErrNoRows {
		return nil, fmt.Errorf("job with ID %d not found", id)
	}

	if err != nil {
		return nil, fmt.Errorf("error querying job: %v", err)
	}

	return &job, nil
}

func prepareQueryCreateBulk(s string, models []*domain.Job) (string, []interface{}) {
	bf := bytes.Buffer{}
	values := make([]interface{}, 0, len(models)*7)

	for i, v := range models {
		values = append(values, v.Title, v.Company,
			v.CompanyLink, v.Location, v.JobLink, v.JobPostTime, v.Provider,
		)

		numFields := 7
		n := i * numFields

		bf.WriteString("(")
		for j := 0; j < numFields; j++ {
			bf.WriteString("$")
			bf.WriteString(strconv.Itoa(n + j + 1))
			if j < numFields-1 {
				bf.WriteString(", ")
			}
		}
		bf.WriteString(")")
		if i < len(models)-1 {
			bf.WriteString(", ")
		}
	}

	return fmt.Sprintf(s, bf.String()), values
}

func (r *JobRepository) GetJobsByStatus(status domain.JobStatus) ([]domain.Job, error) {
	query := `
		SELECT id, title, company, company_link, location, job_link, job_timestamp
		FROM jobs 
		WHERE status = $1
	`

	rows, err := r.db.Query(query, int(status))
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	var jobs []domain.Job
	for rows.Next() {
		var job domain.Job
		err := rows.Scan(
			&job.ID,
			&job.Title,
			&job.Company,
			&job.CompanyLink,
			&job.Location,
			&job.JobLink,
			&job.JobPostTime,
		)
		if err != nil {
			return nil, err
		}
		jobs = append(jobs, job)
	}

	return jobs, rows.Err()
}

func (r *JobRepository) UpdateJobStatus(jobID int64, status domain.JobStatus) error {
	query := `UPDATE jobs SET status = $1 WHERE id = $2`
	_, err := r.db.Exec(query, int(status), jobID)
	return err
}
