package domain

import (
	"errors"
	"time"
)

var (
	ErrTeamMemberNotFound       = errors.New("team member not found")
	ErrTeamMemberEmailExists    = errors.New("team member email already exists")
	ErrInvalidTeamRole          = errors.New("invalid team role")
	ErrInvalidTeamMemberStatus  = errors.New("invalid team member status")
	ErrInvalidEmploymentType    = errors.New("invalid employment type")
	ErrTeamMemberEmailNotVerified = errors.New("team member email not verified")
)

type TeamRole string

const (
	TeamRoleSuperAdmin     TeamRole = "super_admin"
	TeamRoleAdmin          TeamRole = "admin"
	TeamRoleContentManager TeamRole = "content_manager"
	TeamRoleSupportAgent   TeamRole = "support_agent"
	TeamRoleInstructor     TeamRole = "instructor"
	TeamRoleFinance        TeamRole = "finance"
	TeamRoleHR             TeamRole = "hr"
	TeamRoleAnalyst        TeamRole = "analyst"
)

func (r TeamRole) IsValid() bool {
	switch r {
	case TeamRoleSuperAdmin, TeamRoleAdmin, TeamRoleContentManager,
		TeamRoleSupportAgent, TeamRoleInstructor, TeamRoleFinance,
		TeamRoleHR, TeamRoleAnalyst:
		return true
	default:
		return false
	}
}

func (r TeamRole) String() string {
	return string(r)
}

type TeamMemberStatus string

const (
	TeamMemberStatusActive     TeamMemberStatus = "active"
	TeamMemberStatusInactive   TeamMemberStatus = "inactive"
	TeamMemberStatusSuspended  TeamMemberStatus = "suspended"
	TeamMemberStatusTerminated TeamMemberStatus = "terminated"
)

func (s TeamMemberStatus) IsValid() bool {
	switch s {
	case TeamMemberStatusActive, TeamMemberStatusInactive,
		TeamMemberStatusSuspended, TeamMemberStatusTerminated:
		return true
	default:
		return false
	}
}

type EmploymentType string

const (
	EmploymentTypeFullTime EmploymentType = "full_time"
	EmploymentTypePartTime EmploymentType = "part_time"
	EmploymentTypeContract EmploymentType = "contract"
	EmploymentTypeIntern   EmploymentType = "intern"
)

func (e EmploymentType) IsValid() bool {
	switch e {
	case EmploymentTypeFullTime, EmploymentTypePartTime,
		EmploymentTypeContract, EmploymentTypeIntern:
		return true
	default:
		return false
	}
}

type TeamMember struct {
	ID        int64
	FirstName string
	LastName  string
	Email     string
	PhoneNumber string
	Password  []byte
	
	TeamRole       TeamRole
	Department     string
	JobTitle       string
	EmploymentType EmploymentType
	HireDate       *time.Time
	
	ProfilePictureURL string
	Bio               string
	WorkPhone         string
	EmergencyContact  string
	
	Status        TeamMemberStatus
	EmailVerified bool
	Permissions   []string
	
	LastLogin *time.Time
	CreatedBy *int64
	UpdatedBy *int64
	
	CreatedAt time.Time
	UpdatedAt *time.Time
}

type TeamMemberResponse struct {
	ID          int64  `json:"id"`
	FirstName   string `json:"first_name"`
	LastName    string `json:"last_name"`
	Email       string `json:"email"`
	PhoneNumber string `json:"phone_number,omitempty"`
	
	TeamRole       TeamRole       `json:"team_role"`
	Department     string         `json:"department,omitempty"`
	JobTitle       string         `json:"job_title,omitempty"`
	EmploymentType EmploymentType `json:"employment_type,omitempty"`
	HireDate       string         `json:"hire_date,omitempty"`
	
	ProfilePictureURL string   `json:"profile_picture_url,omitempty"`
	Bio               string   `json:"bio,omitempty"`
	WorkPhone         string   `json:"work_phone,omitempty"`
	
	Status        TeamMemberStatus `json:"status"`
	EmailVerified bool             `json:"email_verified"`
	Permissions   []string         `json:"permissions,omitempty"`
	
	LastLogin *time.Time `json:"last_login,omitempty"`
	CreatedAt time.Time  `json:"created_at"`
	UpdatedAt *time.Time `json:"updated_at,omitempty"`
}

type CreateTeamMemberReq struct {
	FirstName   string `json:"first_name" validate:"required"`
	LastName    string `json:"last_name" validate:"required"`
	Email       string `json:"email" validate:"required,email"`
	PhoneNumber string `json:"phone_number"`
	Password    string `json:"password" validate:"required,min=8"`
	
	TeamRole       string `json:"team_role" validate:"required"`
	Department     string `json:"department"`
	JobTitle       string `json:"job_title"`
	EmploymentType string `json:"employment_type"`
	HireDate       string `json:"hire_date"` // YYYY-MM-DD
	
	ProfilePictureURL string   `json:"profile_picture_url"`
	Bio               string   `json:"bio"`
	WorkPhone         string   `json:"work_phone"`
	EmergencyContact  string   `json:"emergency_contact"`
	
	Permissions []string `json:"permissions"`
}

type UpdateTeamMemberReq struct {
	TeamMemberID int64 `json:"-"`
	UpdatedBy    int64 `json:"-"`
	
	FirstName   string `json:"first_name"`
	LastName    string `json:"last_name"`
	PhoneNumber string `json:"phone_number"`
	
	TeamRole       string `json:"team_role"`
	Department     string `json:"department"`
	JobTitle       string `json:"job_title"`
	EmploymentType string `json:"employment_type"`
	HireDate       string `json:"hire_date"`
	
	ProfilePictureURL string   `json:"profile_picture_url"`
	Bio               string   `json:"bio"`
	WorkPhone         string   `json:"work_phone"`
	EmergencyContact  string   `json:"emergency_contact"`
	
	Permissions []string `json:"permissions"`
}

type UpdateTeamMemberStatusReq struct {
	TeamMemberID int64  `json:"-"`
	Status       string `json:"status" validate:"required"`
	UpdatedBy    int64  `json:"-"`
}

type TeamMemberFilter struct {
	TeamRole   *string
	Department *string
	Status     *string
	Search     string
	
	Page     int64
	PageSize int64
}

type TeamMemberStats struct {
	ActiveCount     int64 `json:"active_count"`
	InactiveCount   int64 `json:"inactive_count"`
	SuspendedCount  int64 `json:"suspended_count"`
	TerminatedCount int64 `json:"terminated_count"`
	TotalCount      int64 `json:"total_count"`
}

type TeamMemberLoginReq struct {
	Email    string `json:"email" validate:"required,email"`
	Password string `json:"password" validate:"required"`
}
