package com.lesingle.creation.controller;

import com.lesingle.creation.common.core.Result;
import com.lesingle.creation.common.security.UserPrincipal;
import com.lesingle.creation.dto.tenant.CreateTenantDTO;
import com.lesingle.creation.dto.tenant.UpdateTenantDTO;
import com.lesingle.creation.service.TenantService;
import com.lesingle.creation.vo.tenant.TenantDetailVO;
import com.lesingle.creation.vo.tenant.TenantListVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 租户管理控制器
 */
@Tag(name = "租户管理")
@RestController
@RequestMapping("/api/tenants")
@RequiredArgsConstructor
public class TenantController {

    private final TenantService tenantService;

    @PostMapping
    @Operation(summary = "创建租户")
    @PreAuthorize("hasAuthority('tenant:create')")
    public Result<TenantDetailVO> create(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @RequestBody @Validated CreateTenantDTO dto) {
        Long creatorId = userPrincipal.getUserId();
        TenantDetailVO result = tenantService.create(dto, creatorId);
        return Result.success(result);
    }

    @GetMapping
    @Operation(summary = "租户列表")
    @PreAuthorize("hasAuthority('tenant:read')")
    public Result<com.baomidou.mybatisplus.extension.plugins.pagination.Page<TenantListVO>> pageList(
            @RequestParam(defaultValue = "1") int page,
            @RequestParam(defaultValue = "10") int pageSize) {
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<TenantListVO> result =
                tenantService.pageList(page, pageSize);
        return Result.success(result);
    }

    @GetMapping("/{id}")
    @Operation(summary = "租户详情")
    @PreAuthorize("hasAuthority('tenant:read')")
    public Result<TenantDetailVO> detail(@PathVariable Long id) {
        TenantDetailVO result = tenantService.detail(id);
        return Result.success(result);
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新租户")
    @PreAuthorize("hasAuthority('tenant:update')")
    public Result<TenantDetailVO> update(
            @AuthenticationPrincipal UserPrincipal userPrincipal,
            @PathVariable Long id,
            @RequestBody @Validated UpdateTenantDTO dto) {
        Long modifierId = userPrincipal.getUserId();
        TenantDetailVO result = tenantService.update(id, dto, modifierId);
        return Result.success(result);
    }

    @DeleteMapping("/{id}")
    @Operation(summary = "删除租户")
    @PreAuthorize("hasAuthority('tenant:delete')")
    public Result<Void> delete(@PathVariable Long id) {
        tenantService.delete(id);
        return Result.success();
    }

    @GetMapping("/{id}/menus")
    @Operation(summary = "获取租户菜单")
    @PreAuthorize("hasAuthority('tenant:read')")
    public Result<List<?>> getTenantMenus(@PathVariable Long id) {
        List<?> result = tenantService.getTenantMenus(id);
        return Result.success(result);
    }
}
