package services

import (
	"quyun/v2/providers/job"
	"quyun/v2/providers/jwt"
	jwt_provider "quyun/v2/providers/jwt"
	"quyun/v2/providers/storage"

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

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

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

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

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

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

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

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

		return obj, nil
	}); err != nil {
		return err
	}
	if err := container.Container.Provide(func(
		audit *audit,
		common *common,
		content *content,
		coupon *coupon,
		creator *creator,
		db *gorm.DB,
		notification *notification,
		order *order,
		super *super,
		tenant *tenant,
		user *user,
		wallet *wallet,
	) (contracts.Initial, error) {
		obj := &services{
			audit:        audit,
			common:       common,
			content:      content,
			coupon:       coupon,
			creator:      creator,
			db:           db,
			notification: notification,
			order:        order,
			super:        super,
			tenant:       tenant,
			user:         user,
			wallet:       wallet,
		}
		if err := obj.Prepare(); err != nil {
			return nil, err
		}

		return obj, nil
	}, atom.GroupInitial); err != nil {
		return err
	}
	if err := container.Container.Provide(func(
		jwt *jwt_provider.JWT,
	) (*super, error) {
		obj := &super{
			jwt: jwt,
		}

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

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

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

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