package com.it.rattan.monisuo.controller;

import com.it.rattan.monisuo.common.Result;
import com.it.rattan.monisuo.entity.ColdWallet;
import com.it.rattan.monisuo.service.ColdWalletService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 冷钱包地址接口
 */
@RestController
public class ColdWalletController {

    @Autowired
    private ColdWalletService coldWalletService;

    /**
     * 管理端 - 获取钱包列表
     */
    @GetMapping("/admin/wallet/list")
    public Result<List<ColdWallet>> getList() {
        List<ColdWallet> list = coldWalletService.getList();
        return Result.success(list);
    }

    /**
     * 管理端 - 新增钱包
     */
    @PostMapping("/admin/wallet/create")
    public Result<Void> create(@RequestBody ColdWallet wallet) {
        if (wallet.getName() == null || wallet.getName().isEmpty()) {
            return Result.fail("钱包名称不能为空");
        }
        if (wallet.getAddress() == null || wallet.getAddress().isEmpty()) {
            return Result.fail("钱包地址不能为空");
        }

        try {
            coldWalletService.create(wallet);
            return Result.success("创建成功", null);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 管理端 - 更新钱包
     */
    @PostMapping("/admin/wallet/update")
    public Result<Void> update(@RequestBody ColdWallet wallet) {
        if (wallet.getId() == null) {
            return Result.fail("钱包ID不能为空");
        }

        try {
            coldWalletService.update(wallet);
            return Result.success("更新成功", null);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 管理端 - 删除钱包
     */
    @PostMapping("/admin/wallet/delete")
    public Result<Void> delete(@RequestBody Map<String, Long> params) {
        Long id = params.get("id");
        if (id == null) {
            return Result.fail("钱包ID不能为空");
        }

        try {
            coldWalletService.delete(id);
            return Result.success("删除成功", null);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 管理端 - 设置默认钱包
     */
    @PostMapping("/admin/wallet/setDefault")
    public Result<Void> setDefault(@RequestBody Map<String, Long> params) {
        Long id = params.get("id");
        if (id == null) {
            return Result.fail("钱包ID不能为空");
        }

        try {
            coldWalletService.setDefault(id);
            return Result.success("设置成功", null);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 管理端 - 切换状态
     */
    @PostMapping("/admin/wallet/toggleStatus")
    public Result<Void> toggleStatus(@RequestBody Map<String, Long> params) {
        Long id = params.get("id");
        if (id == null) {
            return Result.fail("钱包ID不能为空");
        }

        try {
            coldWalletService.toggleStatus(id);
            return Result.success("操作成功", null);
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 用户端 - 获取默认钱包地址
     */
    @GetMapping("/api/wallet/default")
    public Result<Map<String, Object>> getDefaultWallet() {
        ColdWallet wallet = coldWalletService.getDefaultWallet();
        if (wallet == null) {
            return Result.fail("系统暂未配置充值地址");
        }

        Map<String, Object> result = new HashMap<>();
        result.put("id", wallet.getId());
        result.put("name", wallet.getName());
        result.put("address", wallet.getAddress());
        result.put("network", wallet.getNetwork());
        return Result.success(result);
    }
}
