import 'dart:io';
import 'dart:convert';
import 'package:ponshu_room_lite/secrets.dart';

void main() async {
  stdout.writeln('Checking available Gemini models via API...');
  final apiKey = Secrets.geminiApiKey;
  
  if (apiKey.isEmpty) {
    stdout.writeln('API Key is empty in Secrets.');
    return;
  }

  // Use v1beta API to list models
  final url = 'https://generativelanguage.googleapis.com/v1beta/models?key=$apiKey';
  
  try {
    final httpClient = HttpClient();
    final request = await httpClient.getUrl(Uri.parse(url));
    final response = await request.close();
    
    if (response.statusCode == 200) {
      final responseBody = await response.transform(utf8.decoder).join();
      final json = jsonDecode(responseBody);
      
      stdout.writeln('\n--- Available Models ---');
      if (json['models'] != null) {
        for (var model in json['models']) {
          // Filter for "generateContent" supported models
          final supportedMethods = model['supportedGenerationMethods'] as List?;
          if (supportedMethods != null && supportedMethods.contains('generateContent')) {
             stdout.writeln('Name: ${model['name']}');
             stdout.writeln('Display Name: ${model['displayName']}');
             stdout.writeln('Description: ${model['description']}');
             stdout.writeln('-------------------------');
          }
        }
      } else {
        stdout.writeln('No models found in response.');
      }
    } else {
      stdout.writeln('Failed to list models. Status Code: ${response.statusCode}');
      final responseBody = await response.transform(utf8.decoder).join();
      stdout.writeln('Response: $responseBody');
    }
    httpClient.close();
  } catch (e) {
    stdout.writeln('Error checking models: $e');
  }
}
