package transformer

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

// Contract to Model conversions
func CreateVendorRequestToModel(req *contract.CreateVendorRequest) *models.CreateVendorRequest {
	return &models.CreateVendorRequest{
		Name:          req.Name,
		Email:         req.Email,
		PhoneNumber:   req.PhoneNumber,
		Address:       req.Address,
		ContactPerson: req.ContactPerson,
		TaxNumber:     req.TaxNumber,
		PaymentTerms:  req.PaymentTerms,
		Notes:         req.Notes,
		IsActive:      req.IsActive,
	}
}

func UpdateVendorRequestToModel(req *contract.UpdateVendorRequest) *models.UpdateVendorRequest {
	return &models.UpdateVendorRequest{
		Name:          req.Name,
		Email:         req.Email,
		PhoneNumber:   req.PhoneNumber,
		Address:       req.Address,
		ContactPerson: req.ContactPerson,
		TaxNumber:     req.TaxNumber,
		PaymentTerms:  req.PaymentTerms,
		Notes:         req.Notes,
		IsActive:      req.IsActive,
	}
}

func ListVendorsRequestToModel(req *contract.ListVendorsRequest) *models.ListVendorsRequest {
	return &models.ListVendorsRequest{
		Page:     req.Page,
		Limit:    req.Limit,
		Search:   req.Search,
		IsActive: req.IsActive,
	}
}

// Model to Contract conversions
func VendorModelResponseToResponse(vendor *models.VendorResponse) *contract.VendorResponse {
	return &contract.VendorResponse{
		ID:             vendor.ID,
		OrganizationID: vendor.OrganizationID,
		Name:           vendor.Name,
		Email:          vendor.Email,
		PhoneNumber:    vendor.PhoneNumber,
		Address:        vendor.Address,
		ContactPerson:  vendor.ContactPerson,
		TaxNumber:      vendor.TaxNumber,
		PaymentTerms:   vendor.PaymentTerms,
		Notes:          vendor.Notes,
		IsActive:       vendor.IsActive,
		CreatedAt:      vendor.CreatedAt,
		UpdatedAt:      vendor.UpdatedAt,
	}
}

func VendorModelResponsesToResponses(vendors []*models.VendorResponse) []contract.VendorResponse {
	if vendors == nil {
		return nil
	}

	responses := make([]contract.VendorResponse, len(vendors))
	for i, vendor := range vendors {
		response := VendorModelResponseToResponse(vendor)
		if response != nil {
			responses[i] = *response
		}
	}
	return responses
}

func ListVendorsModelResponseToResponse(resp *models.ListVendorsResponse) *contract.ListVendorsResponse {
	contractVendors := VendorModelResponsesToResponses(resp.Vendors)
	return &contract.ListVendorsResponse{
		Vendors:    contractVendors,
		TotalCount: resp.TotalCount,
		Page:       resp.Page,
		Limit:      resp.Limit,
		TotalPages: resp.TotalPages,
	}
}
