package transformer

import (
	"apskel-pos-be/internal/contract"
	"apskel-pos-be/internal/models"
)

// Contract to Model conversions
func CreateCustomerRequestToModel(req *contract.CreateCustomerRequest) *models.CreateCustomerRequest {
	return &models.CreateCustomerRequest{
		Name:    req.Name,
		Email:   req.Email,
		Phone:   req.Phone,
		Address: req.Address,
	}
}

func UpdateCustomerRequestToModel(req *contract.UpdateCustomerRequest) *models.UpdateCustomerRequest {
	return &models.UpdateCustomerRequest{
		Name:     req.Name,
		Email:    req.Email,
		Phone:    req.Phone,
		Address:  req.Address,
		IsActive: req.IsActive,
	}
}

func ListCustomersRequestToModel(req *contract.ListCustomersRequest) *models.ListCustomersQuery {
	return &models.ListCustomersQuery{
		Page:      req.Page,
		Limit:     req.Limit,
		Search:    req.Search,
		IsActive:  req.IsActive,
		IsDefault: req.IsDefault,
		SortBy:    req.SortBy,
		SortOrder: req.SortOrder,
	}
}

func SetDefaultCustomerRequestToModel(req *contract.SetDefaultCustomerRequest) *models.SetDefaultCustomerRequest {
	return &models.SetDefaultCustomerRequest{
		CustomerID: req.CustomerID,
	}
}

// Model to Contract conversions
func CustomerModelToResponse(customer *models.CustomerResponse) *contract.CustomerResponse {
	return &contract.CustomerResponse{
		ID:             customer.ID,
		OrganizationID: customer.OrganizationID,
		Name:           customer.Name,
		Email:          customer.Email,
		Phone:          customer.Phone,
		Address:        customer.Address,
		IsDefault:      customer.IsDefault,
		IsActive:       customer.IsActive,
		Metadata:       customer.Metadata,
		CreatedAt:      customer.CreatedAt,
		UpdatedAt:      customer.UpdatedAt,
	}
}

func CustomerResponsesToResponses(customers []models.CustomerResponse) []contract.CustomerResponse {
	responses := make([]contract.CustomerResponse, len(customers))
	for i, customer := range customers {
		response := CustomerModelToResponse(&customer)
		if response != nil {
			responses[i] = *response
		}
	}
	return responses
}

func PaginatedCustomerResponseToContract(paginatedResponse *models.PaginatedResponse[models.CustomerResponse]) *contract.PaginatedCustomerResponse {
	return &contract.PaginatedCustomerResponse{
		Data:       CustomerResponsesToResponses(paginatedResponse.Data),
		TotalCount: int(paginatedResponse.Pagination.Total),
		Page:       paginatedResponse.Pagination.Page,
		Limit:      paginatedResponse.Pagination.Limit,
		TotalPages: paginatedResponse.Pagination.TotalPages,
	}
}
