package client

import (
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
	"gorm.io/gorm/schema"
	"time"
)

type client struct {
	url        string
	dataSource string
	DbEngin    *gorm.DB
}

func (c *client) Task(options TaskOptions) (Task, error) {
	task, _ := newTask(c, &options)
	return task, nil
}

func (c *client) Notice(options NoticeOptions) (Notice, error) {
	notice, _ := newNotice(c, &options)
	return notice, nil
}

func newClient(options Options) (Client, error) {
	//init dbEngine
	dbEngin, _ := gorm.Open(mysql.Open(options.DataSource), &gorm.Config{
		NamingStrategy: schema.NamingStrategy{
			SingularTable: true,
		},
		Logger: logger.Default.LogMode(logger.Warn),
	})
	sqlDB, _ := dbEngin.DB()
	sqlDB.SetMaxIdleConns(10)
	sqlDB.SetMaxOpenConns(50)
	sqlDB.SetConnMaxLifetime(time.Hour)
	c := &client{
		url:        options.Url,
		dataSource: options.DataSource,
		DbEngin:    dbEngin,
	}

	return c, nil
}
