package com.ruoyi.app.user;

import com.ruoyi.app.user.dto.MerchantSubaccountCreateRequest;
import com.ruoyi.app.user.dto.MerchantSubaccountPasswordRequest;
import com.ruoyi.app.user.dto.MerchantSubaccountStatusRequest;
import com.ruoyi.app.user.dto.MerchantSubaccountUpdateRequest;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.utils.Auth;
import com.ruoyi.system.utils.AuthContext;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/merchant/subaccounts")
public class MerchantSubaccountController extends BaseController {

    private final MerchantSubaccountApplicationService applicationService;

    public MerchantSubaccountController(MerchantSubaccountApplicationService applicationService) {
        this.applicationService = applicationService;
    }

    @Anonymous
    @Auth(session = true)
    @GetMapping
    public AjaxResult list(@RequestHeader String token) {
        return success(applicationService.listForOwner(AuthContext.requireUserId()));
    }

    @Anonymous
    @Auth(session = true)
    @PostMapping
    public AjaxResult create(@RequestHeader String token,
                             @RequestBody MerchantSubaccountCreateRequest request) {
        return success(applicationService.create(AuthContext.requireUserId(), request));
    }

    @Anonymous
    @Auth(session = true)
    @PutMapping("/{subaccountUserId}")
    public AjaxResult update(@RequestHeader String token,
                             @PathVariable Long subaccountUserId,
                             @RequestBody MerchantSubaccountUpdateRequest request) {
        return success(applicationService.update(AuthContext.requireUserId(),
                subaccountUserId, request));
    }

    @Anonymous
    @Auth(session = true)
    @PutMapping("/{subaccountUserId}/password")
    public AjaxResult updatePassword(@RequestHeader String token,
                                     @PathVariable Long subaccountUserId,
                                     @RequestBody MerchantSubaccountPasswordRequest request) {
        applicationService.updatePassword(AuthContext.requireUserId(),
                subaccountUserId, request);
        return success();
    }

    @Anonymous
    @Auth(session = true)
    @PutMapping("/{subaccountUserId}/status")
    public AjaxResult updateStatus(@RequestHeader String token,
                                   @PathVariable Long subaccountUserId,
                                   @RequestBody MerchantSubaccountStatusRequest request) {
        applicationService.updateOwnerStatus(AuthContext.requireUserId(),
                subaccountUserId, request);
        return success();
    }
}
