import 'package:block_seasons/game/models/cell.dart';
import 'package:block_seasons/game/models/objective.dart';
import 'package:block_seasons/game/models/stage.dart';
import 'package:flutter_test/flutter_test.dart';

const sampleJson = {
  'id': 's001_007',
  'seed': 8841273,
  'moveLimit': 22,
  'preset': [
    {'x': 3, 'y': 4, 't': 'gem'},
    {'x': 0, 'y': 7, 't': 'filled', 'c': 2},
  ],
  'objectives': [
    {'type': 'clearGems', 'count': 6},
  ],
  'stars': {
    'two': {'movesLeft': 4},
    'three': {'movesLeft': 8},
  },
  'generatorProfile': 'mid',
};

void main() {
  group('StageConfig', () {
    test('parses the season pack stage schema', () {
      final stage = StageConfig.fromJson(sampleJson);
      expect(stage.id, 's001_007');
      expect(stage.seed, 8841273);
      expect(stage.moveLimit, 22);
      expect(stage.preset, hasLength(2));
      expect(stage.preset[0].type, CellType.gem);
      expect(stage.preset[1].colorId, 2);
      expect(stage.objectives.single, isA<Objective>());
      expect(stage.generatorProfile, 'mid');
    });

    test('round-trips to JSON', () {
      final stage = StageConfig.fromJson(sampleJson);
      expect(stage.toJson(), sampleJson);
    });

    test('star thresholds map moves left to stars', () {
      final stage = StageConfig.fromJson(sampleJson);
      expect(stage.stars.starsFor(movesLeft: 0), 1);
      expect(stage.stars.starsFor(movesLeft: 3), 1);
      expect(stage.stars.starsFor(movesLeft: 4), 2);
      expect(stage.stars.starsFor(movesLeft: 7), 2);
      expect(stage.stars.starsFor(movesLeft: 8), 3);
      expect(stage.stars.starsFor(movesLeft: 20), 3);
    });

    test('builds the initial grid from preset cells', () {
      final stage = StageConfig.fromJson(sampleJson);
      final grid = stage.initialGrid();
      expect(grid.cellAt(3, 4).type, CellType.gem);
      expect(grid.cellAt(0, 7).type, CellType.filled);
      expect(grid.cellAt(0, 7).colorId, 2);
      expect(grid.occupiedCount, 2);
    });
  });
}
