package com.reading.platform.controller.admin;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.reading.platform.common.annotation.RequireRole;
import com.reading.platform.common.enums.UserRole;
import com.reading.platform.common.response.PageResult;
import com.reading.platform.common.response.Result;
import com.reading.platform.dto.request.TenantCreateRequest;
import com.reading.platform.dto.request.TenantUpdateRequest;
import com.reading.platform.dto.response.TenantResponse;
import com.reading.platform.entity.Tenant;
import com.reading.platform.service.TenantService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Tag(name = "Admin - Tenant", description = "Tenant Management APIs for Admin")
@RestController
@RequestMapping("/api/admin/tenants")
@RequiredArgsConstructor
@RequireRole(UserRole.ADMIN)
public class AdminTenantController {

    private final TenantService tenantService;

    @Operation(summary = "Create tenant")
    @PostMapping
    public Result<Tenant> createTenant(@Valid @RequestBody TenantCreateRequest request) {
        return Result.success(tenantService.createTenant(request));
    }

    @Operation(summary = "Update tenant")
    @PutMapping("/{id}")
    public Result<Tenant> updateTenant(@PathVariable Long id, @RequestBody TenantUpdateRequest request) {
        return Result.success(tenantService.updateTenant(id, request));
    }

    @Operation(summary = "Get tenant by ID")
    @GetMapping("/{id}")
    public Result<Tenant> getTenant(@PathVariable Long id) {
        return Result.success(tenantService.getTenantById(id));
    }

    @Operation(summary = "Get tenant page")
    @GetMapping
    public Result<PageResult<Tenant>> getTenantPage(
            @RequestParam(required = false) Integer pageNum,
            @RequestParam(required = false) Integer pageSize,
            @RequestParam(required = false) String keyword,
            @RequestParam(required = false) String status) {
        Page<Tenant> page = tenantService.getTenantPage(pageNum, pageSize, keyword, status);
        return Result.success(PageResult.of(page));
    }

    @Operation(summary = "Delete tenant")
    @DeleteMapping("/{id}")
    public Result<Void> deleteTenant(@PathVariable Long id) {
        tenantService.deleteTenant(id);
        return Result.success();
    }

    @Operation(summary = "Get all active tenants")
    @GetMapping("/active")
    public Result<List<TenantResponse>> getAllActiveTenants() {
        return Result.success(tenantService.getAllActiveTenants());
    }

}
