package api

import (
	"ZhenTuLocalPassiveAdapter/config"
	"ZhenTuLocalPassiveAdapter/dto"
	"bytes"
	"context"
	"encoding/json"
	"go.opentelemetry.io/otel/attribute"
	"go.opentelemetry.io/otel/codes"
	"io"
	"log"
	"net/http"
)

func QueryUploadUrlForTask(ctx context.Context, taskId string) (string, error) {
	_, span := tracer.Start(ctx, "QueryUploadUrlForTask")
	defer span.End()
	url := config.Config.Api.BaseUrl + "/" + taskId + "/uploadUrl"
	span.SetAttributes(attribute.String("http.url", url))
	span.SetAttributes(attribute.String("http.method", "POST"))
	req, err := http.NewRequest("POST", url, nil)
	if err != nil {
		span.SetAttributes(attribute.String("error", err.Error()))
		span.SetStatus(codes.Error, "创建请求失败")
		log.Println("Error creating request:", err)
		return "", err
	}
	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		span.SetAttributes(attribute.String("error", err.Error()))
		span.SetStatus(codes.Error, "发送请求失败")
		log.Println("Error sending request:", err)
		return "", err
	}
	span.SetAttributes(attribute.String("http.status", resp.Status))
	span.SetAttributes(attribute.Int("http.status_code", resp.StatusCode))
	defer resp.Body.Close()
	body, err := io.ReadAll(resp.Body)
	if err != nil {
		span.SetAttributes(attribute.String("error", err.Error()))
		span.SetStatus(codes.Error, "读取响应体失败")
		log.Println("Error reading response body:", err)
		return "", err
	}
	return string(body), nil
}

func ReportTaskFailure(ctx context.Context, taskId string) bool {
	_, span := tracer.Start(ctx, "ReportTaskFailure")
	defer span.End()
	url := config.Config.Api.BaseUrl + "/" + taskId + "/failure"
	span.SetAttributes(attribute.String("http.url", url))
	span.SetAttributes(attribute.String("http.method", "POST"))

	req, err := http.NewRequest("POST", url, nil)
	if err != nil {
		span.SetAttributes(attribute.String("error", err.Error()))
		span.SetStatus(codes.Error, "创建请求失败")
		log.Println("Error creating request:", err)
		return false
	}

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		span.SetAttributes(attribute.String("error", err.Error()))
		span.SetStatus(codes.Error, "发送请求失败")
		log.Println("Error sending request:", err)
		return false
	}
	defer resp.Body.Close()

	span.SetAttributes(attribute.String("http.status", resp.Status))
	span.SetAttributes(attribute.Int("http.status_code", resp.StatusCode))
	if resp.StatusCode == 200 {
		span.SetStatus(codes.Ok, "成功")
		return true
	} else {
		span.SetStatus(codes.Error, "失败")
		return false
	}
}

func ReportTaskSuccess(ctx context.Context, taskId string, file *dto.FileObject) (b bool) {
	_, span := tracer.Start(ctx, "ReportTaskSuccess")
	defer span.End()
	url := config.Config.Api.BaseUrl + "/" + taskId + "/success"
	span.SetAttributes(attribute.String("http.url", url))
	span.SetAttributes(attribute.String("http.method", "POST"))

	jsonData, err := json.Marshal(file)
	if err != nil {
		span.SetAttributes(attribute.String("error", err.Error()))
		span.SetStatus(codes.Error, "序列化JSON失败")
		log.Println("Error marshaling JSON:", err)
		return false
	}

	req, err := http.NewRequest("POST", url, bytes.NewBuffer(jsonData))
	if err != nil {
		span.SetAttributes(attribute.String("error", err.Error()))
		span.SetStatus(codes.Error, "创建请求失败")
		log.Println("Error creating request:", err)
		return false
	}
	req.Header.Set("Content-Type", "application/json")

	client := &http.Client{}
	resp, err := client.Do(req)
	if err != nil {
		span.SetAttributes(attribute.String("error", err.Error()))
		span.SetStatus(codes.Error, "发送请求失败")
		log.Println("Error sending request:", err)
		return false
	}
	defer resp.Body.Close()

	span.SetAttributes(attribute.String("http.status", resp.Status))
	span.SetAttributes(attribute.Int("http.status_code", resp.StatusCode))

	if resp.StatusCode == 200 {
		span.SetStatus(codes.Ok, "成功")
		return true
	} else {
		span.SetStatus(codes.Error, "失败")
		return false
	}
}