package request

import (
	"campaign-pool-be/app/database/entity"
	"campaign-pool-be/utils/paginator"
	"time"
)

type ResearchJournalsQueryRequest struct {
	JournalTitle  *string               `json:"journalTitle"`
	Publisher     *string               `json:"publisher"`
	PublishedYear *int                  `json:"publishedYear"`
	Pagination    *paginator.Pagination `json:"pagination"`
}

type ResearchJournalsCreateRequest struct {
	JournalTitle  string     `json:"journalTitle" validate:"required,min=2,max=500"`
	Publisher     string     `json:"publisher" validate:"required,min=2,max=255"`
	JournalURL    string     `json:"journalUrl" validate:"required,url"`
	PublishedDate *time.Time `json:"publishedDate"`
}

func (req ResearchJournalsCreateRequest) ToEntity() *entity.ResearchJournals {
	return &entity.ResearchJournals{
		JournalTitle:  req.JournalTitle,
		Publisher:     req.Publisher,
		JournalURL:    req.JournalURL,
		PublishedDate: req.PublishedDate,
	}
}

type ResearchJournalsUpdateRequest struct {
	JournalTitle  string     `json:"journalTitle" validate:"required,min=2,max=500"`
	Publisher     string     `json:"publisher" validate:"required,min=2,max=255"`
	JournalURL    string     `json:"journalUrl" validate:"required,url"`
	PublishedDate *time.Time `json:"publishedDate"`
}

func (req ResearchJournalsUpdateRequest) ToEntity() *entity.ResearchJournals {
	return &entity.ResearchJournals{
		JournalTitle:  req.JournalTitle,
		Publisher:     req.Publisher,
		JournalURL:    req.JournalURL,
		PublishedDate: req.PublishedDate,
	}
}

type ResearchJournalsQueryRequestContext struct {
	JournalTitle  string `json:"journalTitle"`
	Publisher     string `json:"publisher"`
	PublishedYear string `json:"publishedYear"`
}

func (req ResearchJournalsQueryRequestContext) ToParamRequest() ResearchJournalsQueryRequest {
	var request ResearchJournalsQueryRequest

	if journalTitle := req.JournalTitle; journalTitle != "" {
		request.JournalTitle = &journalTitle
	}
	if publisher := req.Publisher; publisher != "" {
		request.Publisher = &publisher
	}
	if publishedYearStr := req.PublishedYear; publishedYearStr != "" {
		// Extract year from publishedYearStr if it's a date string
		if publishedYearStr != "" {
			// For now, we'll handle this in the repository layer
			// This could be enhanced to parse date strings
		}
	}

	return request
}
