import 'package:flutter_test/flutter_test.dart';
import 'package:recipe_flutter/core/api/api_client.dart';
import 'package:recipe_flutter/features/recipes/data/recipe_repository.dart';

class FakeRecipeApiClient extends ApiClient {
  FakeRecipeApiClient();

  String? lastMethod;
  String? lastPath;
  Object? lastBody;

  @override
  Future<dynamic> getJson(String path, {String? token}) async {
    lastMethod = 'GET';
    lastPath = path;
    return [];
  }

  @override
  Future<dynamic> postJson(String path, {Object? body, String? token}) async {
    lastMethod = 'POST';
    lastPath = path;
    lastBody = body;
    if (path == '/recipes/parse-markdown') {
      return {
        'name': 'Recept',
        'ingredients': [],
      };
    }
    return {
      'id': 1,
      'name': 'Recept',
      'ingredients': [],
    };
  }

  @override
  Future<dynamic> patchJson(String path, {Object? body, String? token}) async {
    lastMethod = 'PATCH';
    lastPath = path;
    lastBody = body;
    return {
      'id': 1,
      'name': 'Recept',
      'ingredients': [],
    };
  }

  @override
  Future<dynamic> deleteJson(String path, {String? token}) async {
    lastMethod = 'DELETE';
    lastPath = path;
    return null;
  }
}

void main() {
  group('RecipeRepository API contract', () {
    test('updateRecipe uses PATCH /recipes/:id', () async {
      final api = FakeRecipeApiClient();
      final repository = RecipeRepository(api);

      await repository.updateRecipe(2, {
        'name': 'Nytt namn',
        'ingredients': [
          {'productId': 1, 'quantity': 1, 'unit': 'st'}
        ],
      });

      expect(api.lastMethod, 'PATCH');
      expect(api.lastPath, '/recipes/2');
    });

    test('deleteRecipe uses DELETE /recipes/:id', () async {
      final api = FakeRecipeApiClient();
      final repository = RecipeRepository(api);

      await repository.deleteRecipe(2);

      expect(api.lastMethod, 'DELETE');
      expect(api.lastPath, '/recipes/2');
    });

    test('parseMarkdown uses POST /recipes/parse-markdown', () async {
      final api = FakeRecipeApiClient();
      final repository = RecipeRepository(api);

      await repository.parseMarkdown('# Test');

      expect(api.lastMethod, 'POST');
      expect(api.lastPath, '/recipes/parse-markdown');
    });
  });
}