package transformer

import (
	"eslogad-be/internal/contract"
	"eslogad-be/internal/entities"
)

func VoteEventToContract(voteEvent *entities.VoteEvent) *contract.VoteEventResponse {
	if voteEvent == nil {
		return nil
	}

	response := &contract.VoteEventResponse{
		ID:           voteEvent.ID,
		Title:        voteEvent.Title,
		Description:  voteEvent.Description,
		StartDate:    voteEvent.StartDate,
		EndDate:      voteEvent.EndDate,
		IsActive:     voteEvent.IsActive,
		ResultsOpen:  voteEvent.ResultsOpen,
		IsVotingOpen: voteEvent.IsVotingOpen(),
		CreatedAt:    voteEvent.CreatedAt,
		UpdatedAt:    voteEvent.UpdatedAt,
	}

	if voteEvent.Candidates != nil && len(voteEvent.Candidates) > 0 {
		response.Candidates = CandidatesToContract(voteEvent.Candidates)
	}

	return response
}

func CandidateToContract(candidate *entities.Candidate) *contract.CandidateResponse {
	if candidate == nil {
		return nil
	}

	return &contract.CandidateResponse{
		ID:          candidate.ID,
		VoteEventID: candidate.VoteEventID,
		Name:        candidate.Name,
		ImageURL:    candidate.ImageURL,
		Description: candidate.Description,
		CreatedAt:   candidate.CreatedAt,
		UpdatedAt:   candidate.UpdatedAt,
	}
}

func CandidatesToContract(candidates []entities.Candidate) []contract.CandidateResponse {
	if candidates == nil {
		return nil
	}

	responses := make([]contract.CandidateResponse, len(candidates))
	for i, c := range candidates {
		resp := CandidateToContract(&c)
		if resp != nil {
			responses[i] = *resp
		}
	}
	return responses
}

func VoteToContract(vote *entities.Vote) *contract.VoteResponse {
	if vote == nil {
		return nil
	}

	return &contract.VoteResponse{
		ID:          vote.ID,
		VoteEventID: vote.VoteEventID,
		CandidateID: vote.CandidateID,
		UserID:      vote.UserID,
		CreatedAt:   vote.CreatedAt,
	}
}