package models

import (
	"time"
)

// User is an agent‑or‑human account that can call ERP tools.
type User struct {
	ID        uint   `gorm:"primaryKey"`
	Email     string `gorm:"size:255;not null;uniqueIndex"`
	Password  string `gorm:"size:255;not null"` // hashed password or credential reference
	Roles     []Role `gorm:"many2many:user_roles;"`
	CreatedAt time.Time
	UpdatedAt time.Time
}

// Note represents a note belonging to a user and is associated with a Service.
type Note struct {
	ID      uint   `gorm:"primaryKey"`
	Title   string `gorm:"size:200;not null"`  // title of the note
	Content string `gorm:"type:text;not null"` // content body

	// Associations
	UserID uint
	User   User `gorm:"foreignKey:UserID"`

	ServiceID uint
	Service   Service `gorm:"foreignKey:ServiceID"`

	CreatedAt time.Time
	UpdatedAt time.Time
}

// Role groups permissions that can be assigned to users or agents.
type Role struct {
	ID          uint         `gorm:"primaryKey"`
	Name        string       `gorm:"size:100;not null;uniqueIndex"`
	Description string       `gorm:"size:255"`
	Permissions []Permission `gorm:"many2many:role_permissions;"`
}

// Permission is a fine‑grained action that can be allowed/denied.
type Permission struct {
	ID          uint   `gorm:"primaryKey"`
	Code        string `gorm:"size:100;not null;uniqueIndex"` // e.g. "invoice:create"
	Description string `gorm:"size:255"`
}

// Service & related entities (agents coordinate work around Services)
type Service struct {
	ID          uint   `gorm:"primaryKey"`
	Name        string `gorm:"size:200;not null"` // human‑readable Service name
	Description string `gorm:"type:text"`

	// Owner/creator of the Service
	CreatedByID uint
	CreatedBy   User `gorm:"foreignKey:CreatedByID"`

	// Participants – many‑to‑many (agents & humans)
	Participants []User `gorm:"many2many:Service_participants;"`

	// Service tasks
	Tasks []Task `gorm:"foreignKey:ServiceID"`

	CreatedAt time.Time
	UpdatedAt time.Time
}

// Task management (assignable work items)
type Task struct {
	ID      uint   `gorm:"primaryKey"`
	Title   string `gorm:"size:200;not null"`
	Content string `gorm:"type:text;not null"`

	// Which service the task belongs to
	ServiceID *uint
	Service   *Service `gorm:"foreignKey:ServiceID"`

	// Who created the task
	CreatedByID uint
	CreatedBy   User `gorm:"foreignKey:CreatedByID"`

	// Who updated the task
	UpdatedByID uint
	UpdatedBy   User `gorm:"foreignKey:UpdatedByID"`

	// Assignment – can be nil (unassigned) or point to a user/agent
	AssigneeID *uint
	Assignee   *User `gorm:"foreignKey:AssigneeID"`

	StatusID uint       // FK to TaskStatus
	Status   TaskStatus `gorm:"foreignKey:StatusID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL;"`

	DueDate  *time.Time
	Priority string `gorm:"size:20"` // e.g. low/medium/high

	CreatedAt time.Time
	UpdatedAt time.Time
}

// Separate table for task status – makes it easy to add custom workflow steps.
type TaskStatus struct {
	ID    uint   `gorm:"primaryKey"`
	Code  string `gorm:"size:50;not null;uniqueIndex"` // e.g. "open", "in_progress", "done"
	Label string `gorm:"size:100"`                     // human‑readable label

	// Reverse relation (optional)
	Tasks []Task `gorm:"foreignKey:StatusID"`

	CreatedAt time.Time
	UpdatedAt time.Time
}

// Message sent inside a conversation.
type Message struct {
	ID       uint      `gorm:"primaryKey"`
	SenderID uint      `gorm:"index;not null"` // user/agent that authored the message
	Sender   User      `gorm:"foreignKey:SenderID"`
	Content  string    `gorm:"type:text;not null"`
	SentAt   time.Time `gorm:"autoCreateTime"`

	// Receivers – many‑to‑many (users who should receive this message)
	Receivers []User `gorm:"many2many:message_receivers;"`

	CreatedAt time.Time
	UpdatedAt time.Time
}
