// SPDX-License-Identifier: Apache-2.0
// Copyright 2025 Sudoku Solver Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef SUDOKU_SOLVER_H
#define SUDOKU_SOLVER_H

#include <vector>
#include <iostream>

/**
 * @brief A comprehensive Sudoku solver using backtracking algorithm
 * 
 * This class provides functionality to solve 9x9 Sudoku puzzles using
 * a backtracking approach with constraint checking optimizations.
 */
class SudokuSolver {
public:
    // Lifecycle
    SudokuSolver();
    ~SudokuSolver();
    
    // Operations
    bool SolvePuzzle(std::vector<std::vector<int>>& grid);
    void PrintGrid(const std::vector<std::vector<int>>& grid) const;
    bool IsValidGrid(const std::vector<std::vector<int>>& grid) const;
    void LoadPuzzle(std::vector<std::vector<int>>& grid, const int puzzle[9][9]);
    
    // Accessors
    int GetSolutionCount() const { return mSolutionCount; }
    int GetBacktrackCount() const { return mBacktrackCount; }
    
private:
    // Constants
    static const int GRID_SIZE = 9;
    static const int SUBGRID_SIZE = 3;
    static const int EMPTY_CELL = 0;
    
    // Attributes
    int mSolutionCount;
    int mBacktrackCount;
    
    // Helper methods
    bool FindEmptyCell(const std::vector<std::vector<int>>& grid, int& row, int& col) const;
    bool IsNumberSafe(const std::vector<std::vector<int>>& grid, int row, int col, int num) const;
    bool IsUsedInRow(const std::vector<std::vector<int>>& grid, int row, int num) const;
    bool IsUsedInCol(const std::vector<std::vector<int>>& grid, int col, int num) const;
    bool IsUsedInSubgrid(const std::vector<std::vector<int>>& grid, int startRow, int startCol, int num) const;
    bool SolveRecursive(std::vector<std::vector<int>>& grid);
    void ResetCounters();
};

#endif // SUDOKU_SOLVER_H