from chessPositions.chesscog.chesscog.recognition import ChessRecognizer
from pathlib import Path
import cv2
import chess

class RecognitionPipeline:
    def __init__(self, classifiers_folder: Path = Path("chesscog/models")):
        self.recognizer = ChessRecognizer(classifiers_folder)

    def image_to_fen(self, image_path: str, turn_white=True, camera_white=True) -> str:
        """
        Converts a chessboard image to a FEN string.

        Args:
            image_path (str): Path to the chessboard image.
            turn_white (bool): True if it's white's turn, False for black.
            camera_white (bool): True if the camera is on white's side.

        Returns:
            str: FEN string representing the board position.
        """
        img = cv2.imread(image_path)
        if img is None:
            raise FileNotFoundError(f"Image not found: {image_path}")
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

        board, *_ = self.recognizer.predict(
            img,
            chess.WHITE if turn_white else chess.BLACK,
            camera_is_white_side=camera_white
        )

        board = self.recognizer._flip_board_horizontally(board)
        return board.board_fen()