package user

import (
	ecore "simrs-vx/internal/domain/base-entities/core"
	"time"

	ep "simrs-vx/internal/domain/main-entities/person"
	epa "simrs-vx/internal/domain/main-entities/person-address"
	epc "simrs-vx/internal/domain/main-entities/person-contact"

	erc "simrs-vx/internal/domain/references/common"
	erg "simrs-vx/internal/domain/references/organization"
)

type CreateDto struct {
	Name                  string                   `json:"name" validate:"maxLength=25"`
	Password              string                   `json:"password" validate:"maxLength=255"`
	Status_Code           erc.UserStatusCode       `json:"status_code" validate:"maxLength=10"`
	Person_Id             *uint                    `json:"-"`
	Person                *ep.UpdateDto            `json:"person"`
	PersonAddresses       []epa.UpdateDto          `json:"personAddresses"`
	PersonContacts        []epc.UpdateDto          `json:"personContacts"`
	Code                  *string                  `json:"code" validate:"maxLength=20"`
	Employee              *EmployeUpdateDto        `json:"employee"`
	IHS_Number            *string                  `json:"ihs_number" validate:"maxLength=20"`
	SIP_Number            *string                  `json:"sip_number" validate:"maxLength=20"`
	Unit_Code             *string                  `json:"unit_code"`
	Infra_Code            *string                  `json:"infra_code"`
	Specialist_Code       *string                  `json:"specialist_code"`
	Subspecialist_Code    *string                  `json:"subspecialist_code"`
	ContractPosition_Code erg.ContractPositionCode `json:"contractPosition_code" gorm:"not null;size:20"`
}

type ReadListDto struct {
	Name        string             `json:"name"`
	Status_Code erc.UserStatusCode `json:"status-code"`
	Pagination  ecore.Pagination
}

type ReadDetailDto struct {
	Id   uint    `json:"id"`
	Name *string `json:"name"`
}

type UpdateDto struct {
	Id uint `json:"id"`
	CreateDto
}

type DeleteDto struct {
	Id uint `json:"id"`
}

type MetaDto struct {
	PageNumber int `json:"page_number"`
	PageSize   int `json:"page_size"`
	Count      int `json:"count"`
}

type LoginDto struct {
	Name     string `json:"name" validate:"required"`
	Password string `json:"password" validate:"required"`
	Duration uint32 `json:"duration"` // in minutes
}

type ResponseDto struct {
	ecore.Main
	Name             string             `json:"name"`
	Status_Code      erc.UserStatusCode `json:"status_code"`
	FailedLoginCount uint8              `json:"failedLoginCount"`
	LastSuccessLogin *time.Time         `json:"lastSuccessLogin,omitempty"`
	LastAllowdLogin  *time.Time         `json:"lastAllowdLogin,omitempty"`
}

func (d *User) ToResponse() ResponseDto {
	resp := ResponseDto{
		Name:             d.Name,
		Status_Code:      d.Status_Code,
		FailedLoginCount: d.FailedLoginCount,
		LastSuccessLogin: d.LastSuccessLogin,
		LastAllowdLogin:  d.LastAllowdLogin,
	}
	resp.Main = d.Main
	return resp
}

type EmployeUpdateDto struct {
	Id            uint                     `json:"id"`
	User_Id       *uint                    `json:"-"`
	Person_Id     *uint                    `json:"-"`
	Division_Code *string                  `json:"division_code"`
	Number        *string                  `json:"number" validate:"maxLength=20"`
	Position_Code erg.EmployeePositionCode `json:"position_code" validate:"maxLength=20"`
	Status_Code   erc.ActiveStatusCode     `json:"status_code" validate:"maxLength=10"`
}

func ToResponseList(data []User) []ResponseDto {
	resp := make([]ResponseDto, len(data))
	for i, u := range data {
		resp[i] = u.ToResponse()
	}
	return resp
}

func (c CreateDto) Sanitize() CreateDto {
	sanitized := c
	sanitized.Password = "[REDACTED]"
	return sanitized
}
