package entities

import (
	"time"

	"github.com/google/uuid"
	"gorm.io/gorm"
)

type ChartOfAccountType struct {
	ID          uuid.UUID `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
	Name        string    `gorm:"not null;size:100" json:"name" validate:"required,min=1,max=100"`
	Code        string    `gorm:"not null;size:10;unique" json:"code" validate:"required,min=1,max=10"`
	Description *string   `gorm:"type:text" json:"description"`
	IsActive    bool      `gorm:"default:true" json:"is_active"`
	CreatedAt   time.Time `gorm:"autoCreateTime" json:"created_at"`
	UpdatedAt   time.Time `gorm:"autoUpdateTime" json:"updated_at"`

	ChartOfAccounts []ChartOfAccount `gorm:"foreignKey:ChartOfAccountTypeID" json:"chart_of_accounts,omitempty"`
}

func (c *ChartOfAccountType) BeforeCreate(tx *gorm.DB) error {
	if c.ID == uuid.Nil {
		c.ID = uuid.New()
	}
	return nil
}

func (ChartOfAccountType) TableName() string {
	return "chart_of_account_types"
}
