package jobs

import (
	"context"
	"os"
	"path/filepath"
	"time"

	"quyun/app/models"
	"quyun/providers/ali"
	"quyun/providers/app"
	"quyun/providers/job"

	. "github.com/riverqueue/river"
	log "github.com/sirupsen/logrus"
	_ "go.ipao.vip/atom"
	"go.ipao.vip/atom/contracts"
	_ "go.ipao.vip/atom/contracts"
)

var _ contracts.JobArgs = (*DownloadFromAliOSS)(nil)

type DownloadFromAliOSS struct {
	MediaHash string `json:"media_hash"`
}

func (s DownloadFromAliOSS) InsertOpts() InsertOpts {
	return InsertOpts{
		Queue:    QueueDefault,
		Priority: PriorityDefault,
	}
}

func (s DownloadFromAliOSS) Kind() string     { return "download_from_ali_oss" }
func (a DownloadFromAliOSS) UniqueID() string { return a.Kind() }

var _ Worker[DownloadFromAliOSS] = (*DownloadFromAliOSSWorker)(nil)

// @provider(job)
type DownloadFromAliOSSWorker struct {
	WorkerDefaults[DownloadFromAliOSS]

	oss *ali.OSSClient
	job *job.Job
	app *app.Config
}

func (w *DownloadFromAliOSSWorker) NextRetry(job *Job[DownloadFromAliOSS]) time.Time {
	return time.Now().Add(30 * time.Second)
}

func (w *DownloadFromAliOSSWorker) Work(ctx context.Context, job *Job[DownloadFromAliOSS]) error {
	log := log.WithField("job", job.Args.Kind())

	log.Infof("[Start] Working on job with strings: %+v", job.Args)
	defer log.Infof("[End] Finished %s", job.Args.Kind())

	media, err := models.Medias.GetByHash(ctx, job.Args.MediaHash)
	if err != nil {
		log.Errorf("Error getting media by ID: %v", err)
		return JobCancel(err)
	}

	dst := filepath.Join(w.app.StoragePath, media.Path)
	// check is path exist
	st, err := os.Stat(dst)
	if os.IsNotExist(err) {
		log.Infof("File not exists: %s", dst)
		err := os.MkdirAll(filepath.Dir(dst), os.ModePerm)
		if err != nil {
			log.Errorf("Error creating directory: %v", err)
			return err
		}
	} else {
		if st.Size() == media.Size {
			return w.NextJob(media.Hash)
		} else {
			// remove file
			if err := os.Remove(dst); err != nil {
				log.Errorf("Error removing file: %v", err)
				return err
			}
		}
	}

	log.Infof("Starting download for file: %s", media.Path)
	if err := w.oss.Download(ctx, media.Path, dst, ali.WithInternal()); err != nil {
		log.Errorf("Error downloading file: %v", err)
		return err
	}

	log.Infof("Successfully downloaded file: %s", media.Path)

	return w.NextJob(media.Hash)
}

func (w *DownloadFromAliOSSWorker) NextJob(hash string) error {
	if err := w.job.Add(&VideoCut{MediaHash: hash}); err != nil {
		log.Errorf("Error adding job: %v", err)
		return err
	}

	return nil
}
