package datasql

import (
	"context"
	"fmt"

	"gitlab.ost.ch/marketminds/marketminds/pkg/entity"
)

func (r *SqlRepository) GetSentimentResultByNewsItemId(newsItemId int64) (*entity.SentimentResult, error) {
	query := `SELECT * FROM marketminds.sentimentresult WHERE "newsitemId"=$1`
	row := r.pool.QueryRow(context.Background(), query, newsItemId)

	var sentimentResult entity.SentimentResult
	err := row.Scan(
		&sentimentResult.Id,
		&sentimentResult.TitlePositive, &sentimentResult.TitleNeutral, &sentimentResult.TitleNegative,
		&sentimentResult.DescriptionPositive, &sentimentResult.DescriptionNeutral, &sentimentResult.DescriptionNegative,
		&sentimentResult.CreatedAt, &sentimentResult.NewItemId)
	if err != nil {
		return nil, err
	}
	return &sentimentResult, nil
}

func (r *SqlRepository) GetAllSentimentResults() ([]entity.SentimentResult, error) {
	rows, err := r.pool.Query(context.Background(), "SELECT * FROM marketminds.sentimentresult")
	if err != nil {
		return nil, err
	}
	defer rows.Close()

	var newsItems []entity.SentimentResult
	for rows.Next() {
		var sentimelResult entity.SentimentResult
		err := rows.Scan(
			&sentimelResult.Id,
			&sentimelResult.TitlePositive, &sentimelResult.TitleNeutral, &sentimelResult.TitleNegative,
			&sentimelResult.DescriptionPositive, &sentimelResult.DescriptionNeutral, &sentimelResult.DescriptionNegative,
			&sentimelResult.CreatedAt, &sentimelResult.NewItemId)
		if err != nil {
			return nil, err
		}
		newsItems = append(newsItems, sentimelResult)
	}
	if err := rows.Err(); err != nil {
		return nil, err
	}
	return newsItems, nil
}

func (r *SqlRepository) CreateSentimentResult(sentimentResult *entity.SentimentResult) error {
	query := `
	insert into marketminds.sentimentresult (
	  titlepositive,
	  titleneutral,
	  titlenegative, 
	  descriptionpostive,
	  descriptionneutral, 
	  descriptionnegative,
	  createdat,
	  "newsitemId"
	)
	VALUES
	( 
	  $1, $2, $3, $4, $5, $6, $7, $8
	)
`
	_, err := r.pool.Exec(context.Background(), query,
		&sentimentResult.TitlePositive, &sentimentResult.TitleNeutral, &sentimentResult.TitleNegative,
		&sentimentResult.DescriptionPositive, &sentimentResult.DescriptionNeutral, &sentimentResult.DescriptionNegative,
		&sentimentResult.CreatedAt, &sentimentResult.NewItemId)

	if err != nil {
		fmt.Println("Error creating sentiment result:", err)
		return err
	}
	fmt.Println("Sentiment result created")
	return nil
}

func (r *SqlRepository) UpdateSentimentResult(sentimelResult *entity.SentimentResult) error {
	query := `
	UPDATE marketminds.sentimentresult
	SET 
	title=$1, 
	description=$2, 
	thumbnailurl=$3, 
	link=$4, 
	pubdate=$5, 
	category=$6, 
	guid=$7, 
	enclosureurl=$8, 
	mediacopyright=$9, 
	mediatitle=$10, 
	version=$11, 
	createdat=$12, 
	newschannelId=$13
	WHERE id=$14
	`
	_, err := r.pool.Exec(context.Background(), query,
		&sentimelResult.Id,
		&sentimelResult.TitlePositive, &sentimelResult.TitleNeutral, &sentimelResult.TitleNegative,
		&sentimelResult.DescriptionPositive, &sentimelResult.DescriptionNeutral, &sentimelResult.DescriptionNegative,
		&sentimelResult.CreatedAt, &sentimelResult.NewItemId)

	if err != nil {
		return err
	}
	return nil
}

func (r *SqlRepository) DeleteSentimentResult(id int) error {
	_, err := r.pool.Exec(context.Background(), "DELETE FROM marketminds.sentimentresult WHERE id=$1", id)
	if err != nil {
		return err
	}
	return nil
}
