package com.example.controller;

import com.example.model.MatrixRequest;
import com.example.service.MatrixService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.concurrent.CompletableFuture;

@RestController
@RequestMapping("/api/matrix")
public class MatrixController {
    private static final Logger logger = LoggerFactory.getLogger(MatrixController.class);

    private final MatrixService matrixService;

    @Autowired
    public MatrixController(MatrixService matrixService) {
        this.matrixService = matrixService;
    }

    @PostMapping("/sort")
    public CompletableFuture<ResponseEntity<int[][]>> sortMatrix(@RequestBody MatrixRequest request) {
        logger.info("Получен запрос на сортировку матрицы: {}x{}",
                request.getMatrix().length, request.getMatrix()[0].length);

        return matrixService.sortColumnsByFirstElementAsync(request.getMatrix())
                .thenApply(sortedMatrix -> {
                    logger.info("Сортировка завершена");
                    return ResponseEntity.ok(sortedMatrix);
                });
    }
}
