package models

import (
	"database/sql"

	"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() (*mediasModel, error) {
		obj := &mediasModel{}
		if err := obj.Prepare(); err != nil {
			return nil, err
		}

		return obj, nil
	}); err != nil {
		return err
	}
	if err := container.Container.Provide(func(
		db *sql.DB,
		medias *mediasModel,
		orders *ordersModel,
		posts *postsModel,
		users *usersModel,
	) (contracts.Initial, error) {
		obj := &models{
			db:     db,
			medias: medias,
			orders: orders,
			posts:  posts,
			users:  users,
		}
		if err := obj.Prepare(); err != nil {
			return nil, err
		}

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

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

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

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