package com.sino.mci.admin.controller;

import com.sino.mci.admin.dto.CreateTenantRequest;
import com.sino.mci.admin.dto.CreateUserRequest;
import com.sino.mci.admin.dto.LoginRequest;
import com.sino.mci.admin.dto.PlatformUserResponse;
import com.sino.mci.admin.dto.TokenResponse;
import com.sino.mci.admin.dto.UpdateTenantRequest;
import com.sino.mci.admin.dto.UpdateUserRequest;
import com.sino.mci.admin.dto.UserResponse;
import com.sino.mci.admin.entity.PlatformRole;
import com.sino.mci.admin.entity.Tenant;
import com.sino.mci.admin.security.AuthContext;
import com.sino.mci.admin.service.AccountService;
import com.sino.mci.shared.web.ApiResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/account")
@RequiredArgsConstructor
public class AccountController {

    private final AccountService accountService;

    @PostMapping("/tenant")
    public ApiResponse<Tenant> createTenant(@Valid @RequestBody CreateTenantRequest request) {
        return ApiResponse.ok(accountService.createTenant(request));
    }

    @PutMapping("/tenant/{id}")
    public ApiResponse<Tenant> updateTenant(
            @PathVariable Long id,
            @Valid @RequestBody UpdateTenantRequest request) {
        return ApiResponse.ok(accountService.updateTenant(id, request));
    }

    @DeleteMapping("/tenant/{id}")
    public ApiResponse<Void> deleteTenant(@PathVariable Long id) {
        accountService.deleteTenant(id);
        return ApiResponse.ok();
    }

    @GetMapping("/tenant/list")
    public ApiResponse<List<Tenant>> listTenants() {
        return ApiResponse.ok(accountService.listTenants());
    }

    @GetMapping("/role/list")
    public ApiResponse<List<PlatformRole>> listRoles() {
        return ApiResponse.ok(accountService.listRoles());
    }

    @PostMapping("/user")
    public ApiResponse<UserResponse> createUser(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @Valid @RequestBody CreateUserRequest request) {
        return ApiResponse.ok(accountService.createUser(tenantCode, request));
    }

    @PutMapping("/user/{id}")
    public ApiResponse<UserResponse> updateUser(
            @PathVariable Long id,
            @Valid @RequestBody UpdateUserRequest request) {
        return ApiResponse.ok(accountService.updateUser(id, request));
    }

    @DeleteMapping("/user/{id}")
    public ApiResponse<Void> deleteUser(@PathVariable Long id) {
        accountService.deleteUser(id);
        return ApiResponse.ok();
    }

    @GetMapping("/user/list")
    public ApiResponse<List<PlatformUserResponse>> listUsers(
            @RequestParam(required = false) String tenantCode) {
        return ApiResponse.ok(accountService.listUsers(tenantCode));
    }

    @PostMapping("/login")
    public ApiResponse<TokenResponse> login(
            @RequestHeader("X-Tenant-Code") String tenantCode,
            @Valid @RequestBody LoginRequest request) {
        return ApiResponse.ok(accountService.login(tenantCode, request.getUsername(), request.getPassword()));
    }

    @GetMapping("/me")
    public ApiResponse<AuthContext.AuthUser> me() {
        return ApiResponse.ok(AuthContext.get());
    }
}
