package admin

import (
	"quyun/providers/ali"
	"quyun/providers/app"
	"quyun/providers/job"
	"quyun/providers/jwt"
	"quyun/providers/wepay"

	"go.ipao.vip/atom"
	"go.ipao.vip/atom/container"
	"go.ipao.vip/atom/contracts"
	"go.ipao.vip/atom/opt"
)

func Provide(opts ...opt.Option) error {
	if err := container.Container.Provide(func(
		jwt *jwt.JWT,
	) (*auth, error) {
		obj := &auth{
			jwt: jwt,
		}

		return obj, nil
	}); err != nil {
		return err
	}
	if err := container.Container.Provide(func(
		oss *ali.OSSClient,
	) (*medias, error) {
		obj := &medias{
			oss: oss,
		}

		return obj, nil
	}); err != nil {
		return err
	}
	if err := container.Container.Provide(func(
		wepay *wepay.Client,
	) (*orders, error) {
		obj := &orders{
			wepay: wepay,
		}

		return obj, nil
	}); err != nil {
		return err
	}
	if err := container.Container.Provide(func() (*posts, error) {
		obj := &posts{}

		return obj, nil
	}); err != nil {
		return err
	}
	if err := container.Container.Provide(func(
		auth *auth,
		medias *medias,
		orders *orders,
		posts *posts,
		statistics *statistics,
		uploads *uploads,
		users *users,
	) (contracts.HttpRoute, error) {
		obj := &Routes{
			auth:       auth,
			medias:     medias,
			orders:     orders,
			posts:      posts,
			statistics: statistics,
			uploads:    uploads,
			users:      users,
		}
		if err := obj.Prepare(); err != nil {
			return nil, err
		}

		return obj, nil
	}, atom.GroupRoutes); err != nil {
		return err
	}
	if err := container.Container.Provide(func() (*statistics, error) {
		obj := &statistics{}

		return obj, nil
	}); err != nil {
		return err
	}
	if err := container.Container.Provide(func(
		app *app.Config,
		job *job.Job,
		oss *ali.OSSClient,
	) (*uploads, error) {
		obj := &uploads{
			app: app,
			job: job,
			oss: oss,
		}

		return obj, nil
	}); err != nil {
		return err
	}
	if err := container.Container.Provide(func() (*users, error) {
		obj := &users{}

		return obj, nil
	}); err != nil {
		return err
	}
	return nil
}
