package personaddress

import (
	ecore "simrs-vx/internal/domain/base-entities/core"
	epr "simrs-vx/internal/domain/main-entities/postal-region"
	ev "simrs-vx/internal/domain/main-entities/village"

	erp "simrs-vx/internal/domain/references/person"
)

type CreateDto struct {
	Person_Id         uint                        `json:"person_id"`
	LocationType_Code erp.AddressLocationTypeCode `json:"locationType_code" validate:"maxLength=10"`
	Address           string                      `json:"address" validate:"maxLength=150"`
	Rt                string                      `json:"rt" validate:"maxLength=2"`
	Rw                string                      `json:"rw" validate:"maxLength=2"`
	PostalRegion_Code *string                     `json:"postalRegion_code" validate:"maxLength=6"`
	Village_Code      *string                     `json:"village_code" validate:"maxLength=10"`
}

type ReadListDto struct {
	Pagination ecore.Pagination
}

type ReadDetailDto struct {
	Id        uint `json:"id"`
	Person_Id uint `json:"person_id"`
}

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 ResponseDto struct {
	ecore.Main
	Person_Id         uint                        `json:"person_id"`
	LocationType_Code erp.AddressLocationTypeCode `json:"locationType_code"`
	Address           string                      `json:"address"`
	Rt                string                      `json:"rt"`
	Rw                string                      `json:"rw"`
	PostalRegion_Code *string                     `json:"postalRegion_code"`
	PostalRegion      *epr.PostalRegion           `json:"postalRegion,omitempty"`
	Village_Code      *string                     `json:"village_code"`
	Village           *ev.Village                 `json:"village,omitempty"`
}

func (d PersonAddress) ToResponse() ResponseDto {
	resp := ResponseDto{
		Person_Id:         d.Person_Id,
		LocationType_Code: d.LocationType_Code,
		Address:           d.Address,
		Rt:                d.Rt,
		Rw:                d.Rw,
		PostalRegion_Code: d.PostalRegion_Code,
		PostalRegion:      d.PostalRegion,
		Village_Code:      d.Village_Code,
		Village:           d.Village,
	}
	resp.Main = d.Main
	return resp
}

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