package service

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

	"web-qudo-be/app/database/entity"
	"web-qudo-be/app/module/about_us_content_images/repository"
	minioStorage "web-qudo-be/config/config"
	"web-qudo-be/utils/storage"

	"github.com/rs/zerolog"
)

type aboutUsContentImageService struct {
	Repo         repository.AboutUsContentImageRepository
	MinioStorage *minioStorage.MinioStorage
	Log          zerolog.Logger
}

type AboutUsContentImageService interface {
	All() (images []*entity.AboutUsContentImage, err error)
	Show(id uint) (image *entity.AboutUsContentImage, err error)
	Save(aboutUsContentId uint, file *multipart.FileHeader) (image *entity.AboutUsContentImage, err error)
	SaveRemoteURL(aboutUsContentID uint, mediaURL, mediaType string) (image *entity.AboutUsContentImage, err error)
	Delete(id uint) error
}

func NewAboutUsContentImageService(
	repo repository.AboutUsContentImageRepository,
	minio *minioStorage.MinioStorage,
	log zerolog.Logger,
) AboutUsContentImageService {
	return &aboutUsContentImageService{
		Repo:         repo,
		MinioStorage: minio,
		Log:          log,
	}
}

func (_i *aboutUsContentImageService) All() (images []*entity.AboutUsContentImage, err error) {
	return _i.Repo.GetAll()
}

func (_i *aboutUsContentImageService) Show(id uint) (image *entity.AboutUsContentImage, err error) {
	return _i.Repo.FindOne(id)
}

func (_i *aboutUsContentImageService) Save(aboutUsContentId uint, file *multipart.FileHeader) (image *entity.AboutUsContentImage, err error) {

	_i.Log.Info().
		Uint("aboutUsContentId", aboutUsContentId).
		Str("filename", file.Filename).
		Msg("upload about us media")

	key, url, err := storage.UploadCMSObject(_i.MinioStorage, "about-us", file, true)
	if err != nil {
		return nil, err
	}

	ext := strings.ToLower(filepath.Ext(file.Filename))
	mt := mime.TypeByExtension(ext)
	if mt == "" {
		if ext == ".mp4" || ext == ".webm" {
			mt = "video/" + strings.TrimPrefix(ext, ".")
		} else {
			mt = "application/octet-stream"
		}
	}

	data := &entity.AboutUsContentImage{
		AboutUsContentID: aboutUsContentId,
		MediaPath:        key,
		MediaType:        mt,
		MediaURL:         url,
	}

	result, err := _i.Repo.Create(data)
	if err != nil {
		_i.Log.Error().Err(err).Msg("failed save to DB")
		return nil, err
	}

	return result, nil
}

func (_i *aboutUsContentImageService) SaveRemoteURL(aboutUsContentID uint, mediaURL, mediaType string) (image *entity.AboutUsContentImage, err error) {
	if strings.TrimSpace(mediaURL) == "" {
		return nil, fmt.Errorf("media_url is required")
	}
	mt := mediaType
	if mt == "" {
		lower := strings.ToLower(mediaURL)
		if strings.HasSuffix(lower, ".mp4") || strings.Contains(lower, "video") {
			mt = "video/mp4"
		} else {
			mt = "image/url"
		}
	}
	data := &entity.AboutUsContentImage{
		AboutUsContentID: aboutUsContentID,
		MediaURL:         mediaURL,
		MediaType:        mt,
	}
	return _i.Repo.Create(data)
}

func (_i *aboutUsContentImageService) Delete(id uint) error {
	return _i.Repo.Delete(id)
}



