package com.gcsc.connection.tenant.controller;

import com.gcsc.connection.common.dto.ApiResponse;
import com.gcsc.connection.tenant.dto.CreateTenantRequest;
import com.gcsc.connection.tenant.dto.TenantResponse;
import com.gcsc.connection.tenant.dto.UpdateTenantRequest;
import com.gcsc.connection.tenant.service.TenantService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.http.ResponseEntity;
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 java.util.List;

/**
 * 테넌트 관리 API
 */
@RestController
@RequestMapping("/api/tenants")
@RequiredArgsConstructor
public class TenantController {

    private final TenantService tenantService;

    /**
     * 전체 테넌트 목록 조회
     */
    @GetMapping
    public ResponseEntity<ApiResponse<List<TenantResponse>>> getTenants() {
        List<TenantResponse> tenants = tenantService.getTenants();
        return ResponseEntity.ok(ApiResponse.ok(tenants));
    }

    /**
     * 테넌트 생성
     */
    @PostMapping
    public ResponseEntity<ApiResponse<TenantResponse>> createTenant(
            @RequestBody @Valid CreateTenantRequest request) {
        TenantResponse tenant = tenantService.createTenant(request);
        return ResponseEntity.ok(ApiResponse.ok(tenant));
    }

    /**
     * 테넌트 수정
     */
    @PutMapping("/{id}")
    public ResponseEntity<ApiResponse<TenantResponse>> updateTenant(
            @PathVariable Long id,
            @RequestBody @Valid UpdateTenantRequest request) {
        TenantResponse tenant = tenantService.updateTenant(id, request);
        return ResponseEntity.ok(ApiResponse.ok(tenant));
    }
}
