import 'package:cinema/feature/websocket/websocket.service.dart';
import 'package:cinema/injectable.dart';
import 'package:injectable/injectable.dart';
import 'package:shelf/shelf.dart';
import 'package:shelf_router/shelf_router.dart';

part 'root.service.g.dart';

@injectable
class RootService {
  @Route.get('/')
  Future<Response> getRoot(Request request) async {
    final isWebSocket =
        request.headers['connection']?.toLowerCase() == 'upgrade' &&
        request.headers['upgrade']?.toLowerCase() == 'websocket';

    if (isWebSocket) {
      return getIt<WebSocketService>().handler(request);
    }

    return Response.ok('REST response');
  }

  Router get router => _$RootServiceRouter(this);
}
