#pragma once
#include <Arduino.h>
#include <Adafruit_ST7789.h>
#include "config.h"

// --- Types ---

enum State
{
    MENU,
    PLAYING,
    AI_TURN,
    FINISHED_WIN,
    FINISHED_DRAW,
    DEMO,
    SETTINGS,
    GAME_LOG
};

struct WinPos
{
    int8_t c, r;
};

struct GameEntry
{
    char type;
    uint8_t level;
    char winner;
    String moves;
};

// --- Shared globals (defined in game.cpp) ---

extern Adafruit_ST7789 tft;

extern int8_t board[COLS][ROWS];
extern WinPos winPos[4];
extern int winCount;

extern State gameState;
extern int8_t menuMode;
extern int8_t currentPlayer;
extern int8_t winnerPlayer;
extern int8_t activeCol;
extern int8_t gameMenuMode;
extern uint8_t demoPly[2];
extern bool abortAi;
extern String currentMoves;
extern uint8_t gameLevel;
extern uint8_t currentLookAhead;
extern bool blunderEnabled;
extern uint8_t blunderChance;

extern GameEntry gameLog[MAX_GAME_LOG];
extern uint8_t gameLogCount;

extern uint32_t lastActivityTime;
extern uint32_t demoResetTimer;
extern uint32_t lastDemoMove;
extern bool flashToggle;
extern uint32_t lastFlash;
extern bool needRedraw;

extern const int8_t colOrder[];

// --- Board helpers ---

uint16_t playerColor(int8_t p);
uint16_t playerColorDim(int8_t p);
int cellX(int c);
int cellY(int r);
void resetBoard();
int getFirstEmptyRow(int col);
bool isBoardFull();
bool isWinPos(int c, int r);
void randomizeDemoPlies();

// --- Game logic ---

int8_t scanBoard();
int evaluateBoard(int8_t aiP, int8_t huP);
bool checkGameEnd();

// --- AI ---

int minimax(int depth, int alpha, int beta, bool isMax, int8_t aiP, int8_t huP);
int computeAiMove(int8_t aiP);
