import 'dart:typed_data';

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

import '../../../api_params.dart';
import '../../../api_response.dart';
import '../talk_api.dart';

/// Small POST/DELETE-only Talk endpoints that have no response payload.
/// Each class extends [TalkApi] with `assemble` returning `null`. They share
/// no state — they're collected here purely to avoid eight near-empty files.

class SetFavorite extends TalkApi {
  final String chatToken;
  final bool favoriteState;

  SetFavorite(this.chatToken, this.favoriteState)
    : super('v4/room/$chatToken/favorite', null);

  @override
  ApiResponse? assemble(String raw) => null;

  @override
  Future<http.Response> request(
    Uri uri,
    ApiParams? body,
    Map<String, String>? headers,
  ) => favoriteState
      ? http.post(uri, headers: headers)
      : http.delete(uri, headers: headers);
}

class LeaveRoom extends TalkApi {
  final String chatToken;

  LeaveRoom(this.chatToken)
    : super('v4/room/$chatToken/participants/self', null);

  @override
  ApiResponse? assemble(String raw) => null;

  @override
  Future<http.Response> request(
    Uri uri,
    ApiParams? body,
    Map<String, String>? headers,
  ) => http.delete(uri, headers: headers);
}

class DeleteMessage extends TalkApi {
  final String chatToken;
  final int messageId;

  DeleteMessage(this.chatToken, this.messageId)
    : super('v1/chat/$chatToken/$messageId', null);

  @override
  ApiResponse? assemble(String raw) => null;

  @override
  Future<http.Response> request(
    Uri uri,
    ApiParams? body,
    Map<String, String>? headers,
  ) => http.delete(uri, headers: headers);
}

class SetRoomAvatar extends TalkApi {
  final String chatToken;
  final Uint8List bytes;
  final String filename;

  SetRoomAvatar(this.chatToken, this.bytes, {this.filename = 'avatar.jpg'})
    : super('v1/room/$chatToken/avatar', null);

  @override
  ApiResponse? assemble(String raw) => null;

  @override
  Future<http.Response> request(
    Uri uri,
    ApiParams? body,
    Map<String, String>? headers,
  ) async {
    final req = http.MultipartRequest('POST', uri)
      ..headers.addAll(headers ?? const {})
      ..files.add(http.MultipartFile.fromBytes('file', bytes, filename: filename));
    final streamed = await req.send();
    return http.Response.fromStream(streamed);
  }
}

class DeleteRoomAvatar extends TalkApi {
  final String chatToken;

  DeleteRoomAvatar(this.chatToken) : super('v1/room/$chatToken/avatar', null);

  @override
  ApiResponse? assemble(String raw) => null;

  @override
  Future<http.Response> request(
    Uri uri,
    ApiParams? body,
    Map<String, String>? headers,
  ) => http.delete(uri, headers: headers);
}
