package vclaimsep

import (
	"bytes"
	"encoding/json"
	"fmt"
	"io"
	"net/http"
	e "simrs-vx/internal/domain/bpjs-entities/vclaim-sep"

	ibpjs "simrs-vx/internal/infra/bpjs"

	"gorm.io/gorm"
)

func CreateSep(input *e.CreateDto, data *e.VclaimSep, tx *gorm.DB) error {
	payload, err := input.RequestPayloadIntoJson()
	if err != nil {
		return err
	}
	req, err := http.NewRequest("POST", ibpjs.O.BaseUrl+"sep", bytes.NewBuffer(payload))
	if err != nil {
		return err
	}
	req.Header.Set("Content-Type", "application/json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return err
	}

	var vresp vclaimResponse
	if err := json.Unmarshal(body, &vresp); err != nil {
		return fmt.Errorf("failed to parse response JSON: %w", err)
	}

	msg := vresp.MetaData.Message

	if err := json.Unmarshal(body, data); err != nil {
		return fmt.Errorf("failed to unmarshal into VclaimSep: %w", err)
	}

	// Save request/response details in DTO for further use
	reqPayload, err := input.RequestPayloadWithEncounterId()
	if err != nil {
		return err
	}
	input.VclaimSepHist.RequestPayload = string(reqPayload)
	input.VclaimSepHist.ResponseBody = string(body)
	input.VclaimSepHist.Message = msg
	input.Number = func() *string {
		number := ""
		if vresp.Response != nil && vresp.Response.Sep != nil {
			number = vresp.Response.Sep.NoSep
		}
		return &number
	}()

	return nil
}

func ReadDetailSep(input *e.ReadDetailDto, data *e.VclaimSep, tx *gorm.DB) error {
	endpoint := fmt.Sprintf("sep/%s", input.Number)
	req, err := http.NewRequest("GET", ibpjs.O.BaseUrl+endpoint, nil)
	if err != nil {
		return err
	}
	req.Header.Set("Content-Type", "application/json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return err
	}

	var detail e.SepResponse
	if err := json.Unmarshal(body, &detail); err != nil {
		return fmt.Errorf("failed to parse response JSON: %w", err)
	}

	data.Detail = detail.Response

	return nil
}

func DeleteSep(input *e.DeleteDto, data *e.VclaimSep, tx *gorm.DB) error {
	payload := e.SepDeleteRequest{}
	payload.Request.TSep.NoSep = *input.Number
	payload.Request.TSep.User = "Coba Ws"

	jsonPayload, err := json.Marshal(payload)
	if err != nil {
		return err
	}

	req, err := http.NewRequest("DELETE", ibpjs.O.BaseUrl+"sep", bytes.NewBuffer(jsonPayload))
	if err != nil {
		return err
	}

	req.Header.Set("Content-Type", "application/json")

	resp, err := http.DefaultClient.Do(req)
	if err != nil {
		return err
	}
	defer resp.Body.Close()

	body, err := io.ReadAll(resp.Body)
	if err != nil {
		return err
	}

	var detail e.SepResponse
	if err := json.Unmarshal(body, &detail); err != nil {
		return fmt.Errorf("failed to parse response JSON: %w", err)
	}

	if detail.MetaData.Message == SepNotFound {
		return fmt.Errorf("sep with number %s not found", *data.Number)
	}

	return nil
}
