package com.seer.teach.user.admin.controller;

import com.seer.teach.common.ResultBean;
import com.seer.teach.user.admin.convert.AdminTenantConvert;
import com.seer.teach.user.admin.request.TenantReq;
import com.seer.teach.user.response.TenantResp;
import com.seer.teach.user.entity.TenantEntity;
import com.seer.teach.user.service.ITenantService;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.DeleteMapping;
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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import jakarta.validation.Valid;
import java.util.List;

/**
 * <p>
 * 租户 前端控制器
 * </p>
 *
 * @author Wang
 * @since 2025-08-01 19:39:18
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/sys/tenant")
@Tag(name = "管理端 - 租户管理")
public class AdminTenantController {

    private final ITenantService tenantService;
    @GetMapping("/list")
    public ResultBean<List<TenantResp>> list() {
        List<TenantEntity> list = tenantService.list();
        return ResultBean.success(AdminTenantConvert.INSTANCE.tenantEntities2TenantRespList(list));
    }


    @PostMapping("/create")
    @Operation(summary = "创建租户")
    public ResultBean<Boolean> create(@Valid @RequestBody TenantReq req) {
        TenantEntity entity = AdminTenantConvert.INSTANCE.tenantReq2TenantEntity(req);
        return ResultBean.success(tenantService.save(entity));
    }

    @PutMapping("/update")
    @Operation(summary = "更新租户")
    public ResultBean<Boolean> update(@Valid @RequestBody TenantReq req) {
        TenantEntity entity = AdminTenantConvert.INSTANCE.tenantReq2TenantEntity(req);
        return ResultBean.success(tenantService.updateById(entity));
    }

    @DeleteMapping("/delete/{id}")
    @Operation(summary = "删除租户")
    public ResultBean<Boolean> delete(@PathVariable Long id) {
        return ResultBean.success(tenantService.removeById(id));
    }

    @GetMapping("/get/{id}")
    @Operation(summary = "根据ID获取租户详情")
    public ResultBean<TenantResp> get(@PathVariable Long id) {
        TenantEntity entity = tenantService.getById(id);
        return ResultBean.success(AdminTenantConvert.INSTANCE.tenantEntity2TenantResp(entity));
    }
}