package requestmodel

import (
	"fmt"

	dbtypes "git-molva.ru/Molva/molva-backend/services/api_gateway/internal/database/types"
)

type (
	Profile struct {
		Id          string `json:"id"`
		Name        string `json:"name"`
		PhoneNumber string `json:"phone_number"`
		Email       string `json:"email"`
	}
)

func (p *Profile) From(msg *dbtypes.Profile) *Profile {
	if msg == nil {
		return nil
	}

	res := &Profile{
		Id:          msg.Id,
		Name:        msg.Name,
		PhoneNumber: msg.PhoneNumber,
		Email:       msg.Email,
	}

	return res
}

type (
	ProfileGetRequest struct {
		Id string `json:"id"`
	}

	ProfileGetResponse struct {
		Profile *Profile `json:"profile"`
	}
)

func (r *ProfileGetRequest) Validate() error {
	if r == nil {
		return fmt.Errorf("%w: request is nil", ErrValidation)
	}

	if r.Id == "" {
		return fmt.Errorf("%w: ID is required", ErrValidation)
	}

	return nil
}

func (r *ProfileGetResponse) From(msg *dbtypes.ProfileGetResponse) *ProfileGetResponse {
	if msg == nil {
		return nil
	}

	return &ProfileGetResponse{
		Profile: new(Profile).From(msg.Profile),
	}
}

type (
	ProfileUpdateRequest struct {
		Id          string
		Name        *string `json:"name"`
		PhoneNumber *string `json:"phone_number"`
		Email       *string `json:"email"`
	}

	ProfileUpdateResponse struct{}
)

func (r *ProfileUpdateRequest) Validate() error {
	if r == nil {
		return fmt.Errorf("%w: request is nil", ErrValidation)
	}

	if r.Id == "" {
		return fmt.Errorf("%w: ID is required", ErrValidation)
	}

	if r.Name == nil && r.PhoneNumber == nil && r.Email == nil {
		return fmt.Errorf("%w: nothing to update", ErrValidation)
	}

	return nil
}
