package com.xiang.xservice.application.script.domain.controller;


import com.xiang.xservice.basic.common.resp.Result;
import com.xiang.xservice.basic.utils.IpUtils;
import com.xiang.xservice.application.script.domain.service.IDomainService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.time.LocalDateTime;

/**
 * 域名动态解析
 *
 * @Author: xiang
 * @Date: 2025-06-10 16:44
 */
@RestController
@RequestMapping("/system/domain")
@Slf4j
@RequiredArgsConstructor
public class DynamicDomainController {

    private final IDomainService IDomainService;

    @GetMapping("/getIp")
    public Result<String> getPublicIp() {
        String publicIp;
        try {
            publicIp = IpUtils.getPublicIp();
        } catch (IOException e) {
            log.error("获取公网IP失败， time:{}", LocalDateTime.now(), e);
            return Result.error("获取公网IP失败");
        }
        return Result.success("获取公网IP成功", publicIp);
    }

    @PostMapping("/ddns")
    public Result<String> dynamicDomainAnalysis() {
        try {
            String publicIp = IpUtils.getPublicIp();
            log.info("获取公网IP成功，time:{}, ip:{}", LocalDateTime.now(), publicIp);
            IDomainService.dynamicDomainAnalysis(publicIp);
            return Result.success("获取公网IP成功");
        } catch (Exception e) {
            log.error("获取公网IP失败， time:{}", LocalDateTime.now(), e);
            return Result.error("获取公网IP失败");
        }
    }
}
