package entities

import (
	"database/sql/driver"
	"encoding/json"
	"errors"
	"time"

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

type UserRole string

const (
	RoleAdmin   UserRole = "admin"
	RoleManager UserRole = "manager"
	RoleCashier UserRole = "cashier"
	RoleWaiter  UserRole = "waiter"
)

type Permissions map[string]interface{}

func (p Permissions) Value() (driver.Value, error) {
	return json.Marshal(p)
}

func (p *Permissions) Scan(value interface{}) error {
	if value == nil {
		*p = make(Permissions)
		return nil
	}

	bytes, ok := value.([]byte)
	if !ok {
		return errors.New("type assertion to []byte failed")
	}

	return json.Unmarshal(bytes, p)
}

type User struct {
	ID             uuid.UUID    `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
	OrganizationID uuid.UUID    `gorm:"type:uuid;not null;index" json:"organization_id" validate:"required"`
	OutletID       *uuid.UUID   `gorm:"type:uuid;index" json:"outlet_id"`
	Name           string       `gorm:"not null;size:255" json:"name" validate:"required,min=1,max=255"`
	Email          string       `gorm:"uniqueIndex;not null;size:255" json:"email" validate:"required,email"`
	PasswordHash   string       `gorm:"not null;size:255" json:"-"`
	Role           UserRole     `gorm:"not null;size:50" json:"role" validate:"required,oneof=admin manager cashier waiter"`
	Permissions    Permissions  `gorm:"type:jsonb;default:'{}'" json:"permissions"`
	IsActive       bool         `gorm:"default:true" json:"is_active"`
	CreatedAt      time.Time    `gorm:"autoCreateTime" json:"created_at"`
	UpdatedAt      time.Time    `gorm:"autoUpdateTime" json:"updated_at"`
	Organization   Organization `gorm:"foreignKey:OrganizationID" json:"organization,omitempty"`
	Outlet         *Outlet      `gorm:"foreignKey:OutletID" json:"outlet,omitempty"`
	Orders         []Order      `gorm:"foreignKey:UserID" json:"orders,omitempty"`
}

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

func (User) TableName() string {
	return "users"
}

func (u *User) HasPermission(permission string) bool {
	if u.Role == RoleAdmin {
		return true
	}

	if value, exists := u.Permissions[permission]; exists {
		if hasPermission, ok := value.(bool); ok {
			return hasPermission
		}
	}

	return false
}

func (u *User) CanAccessOutlet(outletID uuid.UUID) bool {
	if u.Role == RoleAdmin {
		return true
	}

	if u.OutletID != nil && *u.OutletID == outletID {
		return true
	}

	return false
}
