package net.siegeln.cameleer.saas.portal;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PatchMapping;
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;
import java.util.Map;

@RestController
@RequestMapping("/api/tenant")
public class TenantPortalController {

    private final TenantPortalService portalService;

    public TenantPortalController(TenantPortalService portalService) {
        this.portalService = portalService;
    }

    // --- Request bodies ---

    public record InviteRequest(String email, String roleId) {}

    public record RoleChangeRequest(String roleId) {}

    // --- Endpoints ---

    @GetMapping("/dashboard")
    public ResponseEntity<TenantPortalService.DashboardData> getDashboard() {
        return ResponseEntity.ok(portalService.getDashboard());
    }

    @GetMapping("/license")
    public ResponseEntity<TenantPortalService.LicenseData> getLicense() {
        var license = portalService.getLicense();
        if (license == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(license);
    }

    @GetMapping("/oidc")
    public ResponseEntity<Map<String, Object>> getOidcConfig() {
        return ResponseEntity.ok(portalService.getOidcConfig());
    }

    @PutMapping("/oidc")
    public ResponseEntity<Void> updateOidcConfig(@RequestBody Map<String, Object> body) {
        portalService.updateOidcConfig(body);
        return ResponseEntity.ok().build();
    }

    @GetMapping("/team")
    public ResponseEntity<List<Map<String, Object>>> listTeamMembers() {
        return ResponseEntity.ok(portalService.listTeamMembers());
    }

    @PostMapping("/team/invite")
    public ResponseEntity<Map<String, String>> inviteTeamMember(@RequestBody InviteRequest body) {
        String userId = portalService.inviteTeamMember(body.email(), body.roleId());
        return ResponseEntity.ok(Map.of("userId", userId != null ? userId : ""));
    }

    @DeleteMapping("/team/{userId}")
    public ResponseEntity<Void> removeTeamMember(@PathVariable String userId) {
        portalService.removeTeamMember(userId);
        return ResponseEntity.noContent().build();
    }

    @PatchMapping("/team/{userId}/role")
    public ResponseEntity<Void> changeTeamMemberRole(@PathVariable String userId,
                                                      @RequestBody RoleChangeRequest body) {
        portalService.changeTeamMemberRole(userId, body.roleId());
        return ResponseEntity.ok().build();
    }

    @GetMapping("/settings")
    public ResponseEntity<TenantPortalService.TenantSettingsData> getSettings() {
        return ResponseEntity.ok(portalService.getSettings());
    }
}
