package http

import (
	"quyun/providers/ali"
	"quyun/providers/job"
	"quyun/providers/jwt"
	"quyun/providers/wechat"
	"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,
		wechat *wechat.Client,
	) (*auth, error) {
		obj := &auth{
			jwt:    jwt,
			wechat: wechat,
		}

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

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

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

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

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

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