package patient

import (
	"mime/multipart"
	"time"

	ecore "simrs-vx/internal/domain/base-entities/core"
	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"
	epi "simrs-vx/internal/domain/main-entities/person-insurance"
	epr "simrs-vx/internal/domain/main-entities/person-relative"

	erc "simrs-vx/internal/domain/references/common"
	ere "simrs-vx/internal/domain/references/encounter"

	pa "simrs-vx/internal/lib/auth"
)

type CreateDto struct {
	Person_Id              *uint                `json:"-"`
	Person                 *ep.UpdateDto        `json:"person"`
	NewBornStatus          bool                 `json:"newBornStatus"`
	PersonAddresses        []epa.UpdateDto      `json:"personAddresses"`
	PersonContacts         []epc.UpdateDto      `json:"personContacts"`
	PersonRelatives        []epr.UpdateDto      `json:"personRelatives"`
	PersonInsurances       []epi.UpdateDto      `json:"personInsurances"`
	RegisteredAt           *time.Time           `json:"registeredAt"`
	RegisteredBy_User_Name *string              `json:"registeredBy_user_name" validate:"maxLength=100"`
	Status_Code            erc.ActiveStatusCode `json:"status_code" validate:"maxLength=10"`
	Number                 *string              `json:"number"`

	pa.AuthInfo
}

type ReadListDto struct {
	FilterDto
	Includes   string `json:"includes"`
	Pagination ecore.Pagination
}

type FilterDto struct {
	Person_Id     *uint                `json:"person-id"`
	NewBornStatus *bool                `json:"newBornStatus"`
	RegisteredAt  *time.Time           `json:"registeredAt"`
	Status_Code   erc.ActiveStatusCode `json:"status-code"`
	Number        *string              `json:"number"`
}

type ReadDetailDto struct {
	Id        uint16  `json:"id"`
	Person_Id *uint   `json:"person_id"`
	Number    *string `json:"number"`
}

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

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

type SearchDto struct {
	Search     string `json:"search"`
	Pagination ecore.Pagination
}

type UploadDto struct {
	Id           uint                  `json:"-"`
	Code         ere.DocTypeCode       `json:"-"`
	File         multipart.File        `json:"-"`
	FileHeader   *multipart.FileHeader `json:"-"`
	Filename     string                `json:"-"`
	Size         int64                 `json:"-"`
	MimeType     string                `json:"-"`
	MedRecNumber string                `json:"-"`
}

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

type ResponseDto struct {
	ecore.Main
	Person_Id     *uint                `json:"person_id"`
	Person        *ep.Person           `json:"person,omitempty"`
	NewBornStatus bool                 `json:"newBornStatus"`
	RegisteredAt  *time.Time           `json:"registeredAt"`
	Status_Code   erc.ActiveStatusCode `json:"status_code"`
	Number        *string              `json:"number"`
}

func (d Patient) ToResponse() ResponseDto {
	resp := ResponseDto{
		Person_Id:     d.Person_Id,
		Person:        d.Person,
		NewBornStatus: d.NewBornStatus,
		RegisteredAt:  d.RegisteredAt,
		Status_Code:   d.Status_Code,
		Number:        d.Number,
	}
	resp.Main = d.Main
	return resp
}

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