import 'dart:io';
import 'package:http/http.dart' as http;

class AiVoiceService {
  static const String _uploadUrl = 'https://n8n-didvan.liara.run/webhook/upload-mp3';
  static const String _authHeader = 'Basic dXNlcjpkYXNqZHBuM3BjdTQzcDM0aWpyaA==';

  static Future<VoiceUploadResponse> uploadVoice(String filePath) async {
    try {
      final file = File(filePath);
      final bytes = await file.readAsBytes();

      final response = await http.post(
        Uri.parse(_uploadUrl),
        headers: {
          'Content-Type': 'application/octet-stream',
          'Authorization': _authHeader,
          'Accept': '*/*',
        },
        body: bytes,
      );

      if (response.statusCode == 200) {
        // فرض می‌کنیم سرور متن رو برمی‌گردونه
        return VoiceUploadResponse(
          text: response.body,
          isSuccess: true,
        );
      } else {
        return VoiceUploadResponse(
          text: 'خطا در آپلود فایل صوتی',
          isSuccess: false,
        );
      }
    } catch (e) {
      return VoiceUploadResponse(
        text: 'خطا در ارتباط با سرور: ${e.toString()}',
        isSuccess: false,
      );
    }
  }
}

class VoiceUploadResponse {
  final String text;
  final bool isSuccess;

  VoiceUploadResponse({
    required this.text,
    required this.isSuccess,
  });
}
