package cast

import (
	"context"
	"fonchain-fiee/api/cast"
	"fonchain-fiee/pkg/service"
)

func CheckImage(workUuid string) error {
	detailResp, err := service.CastProvider.WorkDetail(context.Background(), &cast.WorkDetailReq{WorkUuid: workUuid})
	if err != nil {
		return err
	}
	if detailResp.CoverUrl == "" {
		return nil
	}
	if detailResp.WorkCategory == 1 {
		return nil
	}
	resourceResp, err := service.CastProvider.WorkResource(context.Background(), &cast.WorkResourceReq{WorkUuid: workUuid})
	if err != nil {
		return err
	}
	if detailResp.CoverUrl == resourceResp.OldCoverUrl {
		return nil
	}
	mediaUrls, err := ProcessImg([]string{detailResp.CoverUrl})
	if err != nil {
		return err
	}
	if len(mediaUrls) > 0 {
		_, _ = service.CastProvider.UpdateWorkResource(context.Background(), &cast.UpdateWorkResourceReq{
			Uuid:        "",
			WorkUuid:    detailResp.WorkUuid,
			OldCoverUrl: detailResp.CoverUrl,
			NewCoverUrl: mediaUrls[0],
		})
	}
	return nil
}
