package domain

import (
	"time"

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

type ClientConsent struct {
	ID            string         `gorm:"primaryKey;type:uuid;default:gen_random_uuid()" json:"id"`
	ClientID      string         `gorm:"index;uniqueIndex:idx_client_subject;not null" json:"clientId"`
	Subject       string         `gorm:"index;uniqueIndex:idx_client_subject;not null" json:"subject"` // User UUID
	GrantedScopes pq.StringArray `gorm:"type:text[];not null" json:"grantedScopes"`
	CreatedAt     time.Time      `json:"createdAt"`
	UpdatedAt     time.Time      `json:"updatedAt"`
	DeletedAt     gorm.DeletedAt `gorm:"index" json:"-"`
}

// ClientConsentWithTenantInfo is a struct to hold joined data for API responses
type ClientConsentWithTenantInfo struct {
	ClientConsent
	TenantID   string `gorm:"column:tenant_id" json:"tenantId"`
	TenantName string `gorm:"column:tenant_name" json:"tenantName"`
}

func (c *ClientConsent) BeforeCreate(tx *gorm.DB) (err error) {
	if c.ID == "" {
		c.ID = uuid.New().String()
	}
	return
}
