package mappers

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

func PurchaseOrderEntityToModel(entity *entities.PurchaseOrder) *models.PurchaseOrder {
	if entity == nil {
		return nil
	}

	return &models.PurchaseOrder{
		ID:              entity.ID,
		OrganizationID:  entity.OrganizationID,
		VendorID:        entity.VendorID,
		PONumber:        entity.PONumber,
		TransactionDate: entity.TransactionDate,
		DueDate:         entity.DueDate,
		Reference:       entity.Reference,
		Status:          entity.Status,
		Message:         entity.Message,
		TotalAmount:     entity.TotalAmount,
		CreatedAt:       entity.CreatedAt,
		UpdatedAt:       entity.UpdatedAt,
	}
}

func PurchaseOrderModelToEntity(model *models.PurchaseOrder) *entities.PurchaseOrder {
	if model == nil {
		return nil
	}

	return &entities.PurchaseOrder{
		ID:              model.ID,
		OrganizationID:  model.OrganizationID,
		VendorID:        model.VendorID,
		PONumber:        model.PONumber,
		TransactionDate: model.TransactionDate,
		DueDate:         model.DueDate,
		Reference:       model.Reference,
		Status:          model.Status,
		Message:         model.Message,
		TotalAmount:     model.TotalAmount,
		CreatedAt:       model.CreatedAt,
		UpdatedAt:       model.UpdatedAt,
	}
}

func PurchaseOrderEntityToResponse(entity *entities.PurchaseOrder) *models.PurchaseOrderResponse {
	if entity == nil {
		return nil
	}

	response := &models.PurchaseOrderResponse{
		ID:              entity.ID,
		OrganizationID:  entity.OrganizationID,
		VendorID:        entity.VendorID,
		PONumber:        entity.PONumber,
		TransactionDate: entity.TransactionDate,
		DueDate:         entity.DueDate,
		Reference:       entity.Reference,
		Status:          entity.Status,
		Message:         entity.Message,
		TotalAmount:     entity.TotalAmount,
		CreatedAt:       entity.CreatedAt,
		UpdatedAt:       entity.UpdatedAt,
	}

	// Map vendor if present
	if entity.Vendor != nil {
		response.Vendor = VendorEntityToResponse(entity.Vendor)
	}

	// Map items if present
	if entity.Items != nil {
		response.Items = PurchaseOrderItemEntitiesToResponses(entity.Items)
	}

	// Map attachments if present
	if entity.Attachments != nil {
		response.Attachments = PurchaseOrderAttachmentEntitiesToResponses(entity.Attachments)
	}

	return response
}

func PurchaseOrderItemEntityToModel(entity *entities.PurchaseOrderItem) *models.PurchaseOrderItem {
	if entity == nil {
		return nil
	}

	return &models.PurchaseOrderItem{
		ID:              entity.ID,
		PurchaseOrderID: entity.PurchaseOrderID,
		IngredientID:    entity.IngredientID,
		Description:     entity.Description,
		Quantity:        entity.Quantity,
		UnitID:          entity.UnitID,
		Amount:          entity.Amount,
		CreatedAt:       entity.CreatedAt,
		UpdatedAt:       entity.UpdatedAt,
	}
}

func PurchaseOrderItemModelToEntity(model *models.PurchaseOrderItem) *entities.PurchaseOrderItem {
	if model == nil {
		return nil
	}

	return &entities.PurchaseOrderItem{
		ID:              model.ID,
		PurchaseOrderID: model.PurchaseOrderID,
		IngredientID:    model.IngredientID,
		Description:     model.Description,
		Quantity:        model.Quantity,
		UnitID:          model.UnitID,
		Amount:          model.Amount,
		CreatedAt:       model.CreatedAt,
		UpdatedAt:       model.UpdatedAt,
	}
}

func PurchaseOrderItemEntityToResponse(entity *entities.PurchaseOrderItem) *models.PurchaseOrderItemResponse {
	if entity == nil {
		return nil
	}

	response := &models.PurchaseOrderItemResponse{
		ID:              entity.ID,
		PurchaseOrderID: entity.PurchaseOrderID,
		IngredientID:    entity.IngredientID,
		Description:     entity.Description,
		Quantity:        entity.Quantity,
		UnitID:          entity.UnitID,
		Amount:          entity.Amount,
		CreatedAt:       entity.CreatedAt,
		UpdatedAt:       entity.UpdatedAt,
	}

	// Map ingredient if present
	if entity.Ingredient != nil {
		response.Ingredient = &models.IngredientResponse{
			ID:   entity.Ingredient.ID,
			Name: entity.Ingredient.Name,
		}
	}

	// Map unit if present
	if entity.Unit != nil {
		response.Unit = &models.UnitResponse{
			ID:   entity.Unit.ID,
			Name: entity.Unit.Name,
		}
	}

	return response
}

func PurchaseOrderAttachmentEntityToModel(entity *entities.PurchaseOrderAttachment) *models.PurchaseOrderAttachment {
	if entity == nil {
		return nil
	}

	return &models.PurchaseOrderAttachment{
		ID:              entity.ID,
		PurchaseOrderID: entity.PurchaseOrderID,
		FileID:          entity.FileID,
		CreatedAt:       entity.CreatedAt,
	}
}

func PurchaseOrderAttachmentModelToEntity(model *models.PurchaseOrderAttachment) *entities.PurchaseOrderAttachment {
	if model == nil {
		return nil
	}

	return &entities.PurchaseOrderAttachment{
		ID:              model.ID,
		PurchaseOrderID: model.PurchaseOrderID,
		FileID:          model.FileID,
		CreatedAt:       model.CreatedAt,
	}
}

func PurchaseOrderAttachmentEntityToResponse(entity *entities.PurchaseOrderAttachment) *models.PurchaseOrderAttachmentResponse {
	if entity == nil {
		return nil
	}

	response := &models.PurchaseOrderAttachmentResponse{
		ID:              entity.ID,
		PurchaseOrderID: entity.PurchaseOrderID,
		FileID:          entity.FileID,
		CreatedAt:       entity.CreatedAt,
	}

	// Map file if present
	if entity.File != nil {
		response.File = &models.FileResponse{
			ID:           entity.File.ID,
			FileName:     entity.File.FileName,
			OriginalName: entity.File.OriginalName,
			FileURL:      entity.File.FileURL,
			FileSize:     entity.File.FileSize,
			MimeType:     entity.File.MimeType,
			FileType:     entity.File.FileType,
			IsPublic:     entity.File.IsPublic,
			CreatedAt:    entity.File.CreatedAt,
			UpdatedAt:    entity.File.UpdatedAt,
		}
	}

	return response
}

// Batch conversion methods
func PurchaseOrderEntitiesToModels(entities []*entities.PurchaseOrder) []*models.PurchaseOrder {
	if entities == nil {
		return nil
	}

	models := make([]*models.PurchaseOrder, len(entities))
	for i, entity := range entities {
		models[i] = PurchaseOrderEntityToModel(entity)
	}
	return models
}

func PurchaseOrderEntitiesToResponses(entities []*entities.PurchaseOrder) []models.PurchaseOrderResponse {
	if entities == nil {
		return nil
	}

	responses := make([]models.PurchaseOrderResponse, len(entities))
	for i, entity := range entities {
		response := PurchaseOrderEntityToResponse(entity)
		if response != nil {
			responses[i] = *response
		}
	}
	return responses
}

func PurchaseOrderItemEntitiesToResponses(entities []entities.PurchaseOrderItem) []models.PurchaseOrderItemResponse {
	if entities == nil {
		return nil
	}

	responses := make([]models.PurchaseOrderItemResponse, len(entities))
	for i, entity := range entities {
		response := PurchaseOrderItemEntityToResponse(&entity)
		if response != nil {
			responses[i] = *response
		}
	}
	return responses
}

func PurchaseOrderAttachmentEntitiesToResponses(entities []entities.PurchaseOrderAttachment) []models.PurchaseOrderAttachmentResponse {
	if entities == nil {
		return nil
	}

	responses := make([]models.PurchaseOrderAttachmentResponse, len(entities))
	for i, entity := range entities {
		response := PurchaseOrderAttachmentEntityToResponse(&entity)
		if response != nil {
			responses[i] = *response
		}
	}
	return responses
}
