import 'package:auto_route/auto_route.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

import '../../../../../application/order/order_loader/order_loader_bloc.dart';
import '../../../../../common/theme/theme.dart';
import '../../../../components/error/order_loader_error_state_widget.dart';
import '../../../../components/loader/loader_with_text.dart';
import 'widgets/refund_success_left_panel.dart';
import 'widgets/refund_success_right_panel.dart';

@RoutePage()
class RefundSuccessPage extends StatelessWidget implements AutoRouteWrapper {
  final String orderId;
  const RefundSuccessPage({super.key, required this.orderId});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: AppColor.background,
      body: SafeArea(
        child: BlocBuilder<OrderLoaderBloc, OrderLoaderState>(
          builder: (context, state) {
            if (state.isFetchingById) {
              return const Center(child: LoaderWithText());
            }
            return state.failureOptionGetById.fold(
              () => Container(
                padding: const EdgeInsets.all(24.0),
                child: Row(
                  children: [
                    // Left Panel - Success Message & Order Info
                    Expanded(
                      flex: 35,
                      child: RefundSuccessLeftPanel(order: state.order),
                    ),

                    const SizedBox(width: 16),

                    // Right Panel - Order Details
                    Expanded(
                      flex: 65,
                      child: RefundSuccessRightPanel(order: state.order),
                    ),
                  ],
                ),
              ),
              (f) => OrderLoaderErrorStateWidget(
                failure: f,
                onRefresh: () {
                  context.read<OrderLoaderBloc>().add(
                    OrderLoaderEvent.getById(orderId),
                  );
                },
              ),
            );
          },
        ),
      ),
    );
  }

  @override
  Widget wrappedRoute(BuildContext context) {
    context.read<OrderLoaderBloc>().add(OrderLoaderEvent.getById(orderId));
    return this;
  }
}
