package uploadhelper

import (
	"fmt"
	"path/filepath"
	"strings"

	ere "simrs-vx/internal/domain/references/encounter"
)

func getBucketForType(docType string) (string, error) {
	switch strings.ToLower(docType) {
	case "resident", "resident-number", "ktp":
		return string(ere.DTCPRN), nil
	case "driver-license", "sim", "license":
		return string(ere.DTCPDL), nil
	case "passport", "paspor":
		return string(ere.DTCPP), nil
	case "family-card", "kk", "family":
		return string(ere.DTCPFC), nil
	case "mcu", "medical", "mcu-result":
		return string(ere.DTCMIR), nil
	default:
		return "", fmt.Errorf("unknown document type: %s", docType)
	}
}

// getValidFileTypesForBucket returns allowed file types for each bucket
func getValidFileTypesForBucket(bucketName string) []string {
	switch bucketName {
	case string(ere.DTCPRN), string(ere.DTCPDL), string(ere.DTCPP), string(ere.DTCPFC):
		return []string{".jpg", ".jpeg", ".png", ".pdf", ".gif"}
	case string(ere.DTCMIR):
		return []string{".jpg", ".jpeg", ".png", ".pdf", ".gif", ".doc", ".docx", ".xls", ".xlsx"}
	default:
		return []string{".jpg", ".jpeg", ".png", ".pdf"}
	}
}

// isValidFileType checks if the uploaded file type is allowed for the specific bucket
func IsValidFileType(ext, bucketName string) bool {
	allowedTypes := getValidFileTypesForBucket(bucketName)

	for _, allowedExt := range allowedTypes {
		if ext == allowedExt {
			return true
		}
	}
	return false
}

// getContentType determines the content type based on file extension
func getContentType(filename string) string {
	switch strings.ToLower(filepath.Ext(filename)) {
	case ".jpg", ".jpeg":
		return "image/jpeg"
	case ".png":
		return "image/png"
	case ".pdf":
		return "application/pdf"
	case ".gif":
		return "image/gif"
	case ".doc":
		return "application/msword"
	case ".docx":
		return "application/vnd.openxmlformats-officedocument.wordprocessingml.document"
	case ".xls":
		return "application/vnd.ms-excel"
	case ".xlsx":
		return "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
	default:
		return "application/octet-stream"
	}
}
