/**
 * @jest-environment jsdom
 */

import { Pawn } from '../../../js/pieces/Pawn.js';
import { Board } from '../../../js/game/Board.js';

describe('Pawn', () => {
  let board;

  beforeEach(() => {
    board = new Board();
    board.clear(); // Start with empty board
  });

  describe('Initial Two-Square Move', () => {
    test('white pawn can move two squares from starting position', () => {
      const pawn = new Pawn('white', { row: 6, col: 4 });
      board.setPiece(6, 4, pawn);

      const moves = pawn.getValidMoves(board);

      expect(moves).toContainEqual({ row: 5, col: 4 });
      expect(moves).toContainEqual({ row: 4, col: 4 });
    });

    test('black pawn can move two squares from starting position', () => {
      const pawn = new Pawn('black', { row: 1, col: 4 });
      board.setPiece(1, 4, pawn);

      const moves = pawn.getValidMoves(board);

      expect(moves).toContainEqual({ row: 2, col: 4 });
      expect(moves).toContainEqual({ row: 3, col: 4 });
    });

    test('pawn cannot move two squares if not at starting position', () => {
      const pawn = new Pawn('white', { row: 5, col: 4 });
      board.setPiece(5, 4, pawn);

      const moves = pawn.getValidMoves(board);

      expect(moves).toContainEqual({ row: 4, col: 4 });
      expect(moves).not.toContainEqual({ row: 3, col: 4 });
    });

    test('pawn cannot move two squares if path blocked', () => {
      const whitePawn = new Pawn('white', { row: 6, col: 4 });
      const blockingPiece = { type: 'knight', color: 'white', position: { row: 5, col: 4 } };

      board.setPiece(6, 4, whitePawn);
      board.setPiece(5, 4, blockingPiece);

      const moves = whitePawn.getValidMoves(board);

      expect(moves).toHaveLength(0);
    });

    test('pawn cannot move two squares if target square blocked', () => {
      const whitePawn = new Pawn('white', { row: 6, col: 4 });
      const blockingPiece = { type: 'knight', color: 'black', position: { row: 4, col: 4 } };

      board.setPiece(6, 4, whitePawn);
      board.setPiece(4, 4, blockingPiece);

      const moves = whitePawn.getValidMoves(board);

      expect(moves).toContainEqual({ row: 5, col: 4 });
      expect(moves).not.toContainEqual({ row: 4, col: 4 });
    });
  });

  describe('Single-Square Forward Move', () => {
    test('white pawn can move one square forward', () => {
      const pawn = new Pawn('white', { row: 5, col: 4 });
      board.setPiece(5, 4, pawn);

      const moves = pawn.getValidMoves(board);

      expect(moves).toContainEqual({ row: 4, col: 4 });
    });

    test('black pawn can move one square forward', () => {
      const pawn = new Pawn('black', { row: 2, col: 4 });
      board.setPiece(2, 4, pawn);

      const moves = pawn.getValidMoves(board);

      expect(moves).toContainEqual({ row: 3, col: 4 });
    });

    test('pawn cannot move forward if blocked', () => {
      const whitePawn = new Pawn('white', { row: 5, col: 4 });
      const blockingPiece = { type: 'knight', color: 'black', position: { row: 4, col: 4 } };

      board.setPiece(5, 4, whitePawn);
      board.setPiece(4, 4, blockingPiece);

      const moves = whitePawn.getValidMoves(board);

      expect(moves).toHaveLength(0);
    });
  });

  describe('Diagonal Captures', () => {
    test('white pawn can capture diagonally', () => {
      const whitePawn = new Pawn('white', { row: 5, col: 4 });
      const blackPiece1 = { type: 'pawn', color: 'black', position: { row: 4, col: 3 } };
      const blackPiece2 = { type: 'pawn', color: 'black', position: { row: 4, col: 5 } };

      board.setPiece(5, 4, whitePawn);
      board.setPiece(4, 3, blackPiece1);
      board.setPiece(4, 5, blackPiece2);

      const moves = whitePawn.getValidMoves(board);

      expect(moves).toContainEqual({ row: 4, col: 3 });
      expect(moves).toContainEqual({ row: 4, col: 5 });
    });

    test('black pawn can capture diagonally', () => {
      const blackPawn = new Pawn('black', { row: 2, col: 4 });
      const whitePiece1 = { type: 'pawn', color: 'white', position: { row: 3, col: 3 } };
      const whitePiece2 = { type: 'pawn', color: 'white', position: { row: 3, col: 5 } };

      board.setPiece(2, 4, blackPawn);
      board.setPiece(3, 3, whitePiece1);
      board.setPiece(3, 5, whitePiece2);

      const moves = blackPawn.getValidMoves(board);

      expect(moves).toContainEqual({ row: 3, col: 3 });
      expect(moves).toContainEqual({ row: 3, col: 5 });
    });

    test('pawn cannot capture own piece', () => {
      const whitePawn = new Pawn('white', { row: 5, col: 4 });
      const whitePiece = { type: 'pawn', color: 'white', position: { row: 4, col: 3 } };

      board.setPiece(5, 4, whitePawn);
      board.setPiece(4, 3, whitePiece);

      const moves = whitePawn.getValidMoves(board);

      expect(moves).not.toContainEqual({ row: 4, col: 3 });
    });

    test('pawn cannot capture forward', () => {
      const whitePawn = new Pawn('white', { row: 5, col: 4 });
      const blackPiece = { type: 'pawn', color: 'black', position: { row: 4, col: 4 } };

      board.setPiece(5, 4, whitePawn);
      board.setPiece(4, 4, blackPiece);

      const moves = whitePawn.getValidMoves(board);

      expect(moves).toHaveLength(0);
    });

    test('pawn on edge can only capture on one side', () => {
      const whitePawn = new Pawn('white', { row: 5, col: 0 });
      const blackPiece = { type: 'pawn', color: 'black', position: { row: 4, col: 1 } };

      board.setPiece(5, 0, whitePawn);
      board.setPiece(4, 1, blackPiece);

      const moves = whitePawn.getValidMoves(board);

      expect(moves).toContainEqual({ row: 4, col: 1 });
      expect(moves).toContainEqual({ row: 4, col: 0 }); // Forward move
      expect(moves).toHaveLength(2);
    });
  });

  describe('En Passant', () => {
    test('white pawn can capture en passant', () => {
      const whitePawn = new Pawn('white', { row: 3, col: 4 });
      const blackPawn = new Pawn('black', { row: 3, col: 5 });
      blackPawn.justMovedTwo = true; // Mark as just moved two squares

      board.setPiece(3, 4, whitePawn);
      board.setPiece(3, 5, blackPawn);

      const gameState = {
        lastMove: {
          piece: blackPawn,
          from: { row: 1, col: 5 },
          to: { row: 3, col: 5 }
        }
      };

      const moves = whitePawn.getValidMoves(board, gameState);

      expect(moves).toContainEqual({ row: 2, col: 5, enPassant: true });
    });

    test('black pawn can capture en passant', () => {
      const blackPawn = new Pawn('black', { row: 4, col: 4 });
      const whitePawn = new Pawn('white', { row: 4, col: 3 });
      whitePawn.justMovedTwo = true;

      board.setPiece(4, 4, blackPawn);
      board.setPiece(4, 3, whitePawn);

      const gameState = {
        lastMove: {
          piece: whitePawn,
          from: { row: 6, col: 3 },
          to: { row: 4, col: 3 }
        }
      };

      const moves = blackPawn.getValidMoves(board, gameState);

      expect(moves).toContainEqual({ row: 5, col: 3, enPassant: true });
    });

    test('en passant only available immediately after two-square move', () => {
      const whitePawn = new Pawn('white', { row: 3, col: 4 });
      const blackPawn = new Pawn('black', { row: 3, col: 5 });

      board.setPiece(3, 4, whitePawn);
      board.setPiece(3, 5, blackPawn);

      const gameState = {
        lastMove: {
          piece: { type: 'knight', color: 'white' }, // Different piece moved last
          from: { row: 7, col: 1 },
          to: { row: 5, col: 2 }
        }
      };

      const moves = whitePawn.getValidMoves(board, gameState);

      expect(moves).not.toContainEqual({ row: 2, col: 5, enPassant: true });
    });

    test('en passant not available for single-square pawn move', () => {
      const whitePawn = new Pawn('white', { row: 3, col: 4 });
      const blackPawn = new Pawn('black', { row: 3, col: 5 });

      board.setPiece(3, 4, whitePawn);
      board.setPiece(3, 5, blackPawn);

      const gameState = {
        lastMove: {
          piece: blackPawn,
          from: { row: 2, col: 5 }, // Moved only one square
          to: { row: 3, col: 5 }
        }
      };

      const moves = whitePawn.getValidMoves(board, gameState);

      expect(moves).not.toContainEqual({ row: 2, col: 5, enPassant: true });
    });
  });

  describe('Promotion', () => {
    test('white pawn on rank 7 can promote', () => {
      const pawn = new Pawn('white', { row: 1, col: 4 });
      expect(pawn.canPromote()).toBe(false);

      pawn.position = { row: 0, col: 4 };
      expect(pawn.canPromote()).toBe(true);
    });

    test('black pawn on rank 2 can promote', () => {
      const pawn = new Pawn('black', { row: 6, col: 4 });
      expect(pawn.canPromote()).toBe(false);

      pawn.position = { row: 7, col: 4 };
      expect(pawn.canPromote()).toBe(true);
    });

    test('white pawn reaching rank 8 must promote', () => {
      const pawn = new Pawn('white', { row: 1, col: 4 });
      board.setPiece(1, 4, pawn);

      const moves = pawn.getValidMoves(board);
      const promotionMove = moves.find(m => m.row === 0 && m.col === 4);

      expect(promotionMove).toBeDefined();
      expect(promotionMove.promotion).toBe(true);
    });

    test('promotion available for capture moves too', () => {
      const whitePawn = new Pawn('white', { row: 1, col: 4 });
      const blackPiece = { type: 'rook', color: 'black', position: { row: 0, col: 5 } };

      board.setPiece(1, 4, whitePawn);
      board.setPiece(0, 5, blackPiece);

      const moves = whitePawn.getValidMoves(board);
      const capturePromotionMove = moves.find(m => m.row === 0 && m.col === 5);

      expect(capturePromotionMove).toBeDefined();
      expect(capturePromotionMove.promotion).toBe(true);
    });
  });

  describe('Edge Cases', () => {
    test('pawn at top rank cannot move (white)', () => {
      const pawn = new Pawn('white', { row: 0, col: 4 });
      board.setPiece(0, 4, pawn);

      const moves = pawn.getValidMoves(board);
      expect(moves).toHaveLength(0);
    });

    test('pawn at bottom rank cannot move (black)', () => {
      const pawn = new Pawn('black', { row: 7, col: 4 });
      board.setPiece(7, 4, pawn);

      const moves = pawn.getValidMoves(board);
      expect(moves).toHaveLength(0);
    });

    test('pawn in corner has limited capture options', () => {
      const whitePawn = new Pawn('white', { row: 5, col: 0 });
      const blackPiece = { type: 'knight', color: 'black', position: { row: 4, col: 1 } };

      board.setPiece(5, 0, whitePawn);
      board.setPiece(4, 1, blackPiece);

      const moves = whitePawn.getValidMoves(board);

      // Can move forward and capture to the right only
      expect(moves).toContainEqual({ row: 4, col: 0 });
      expect(moves).toContainEqual({ row: 4, col: 1 });
      expect(moves).toHaveLength(2);
    });
  });
});
