import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:niogu_ecommerce_v1/core/constant/app_font_size.dart';
import 'package:niogu_ecommerce_v1/core/enums/action_type.dart';
import 'package:niogu_ecommerce_v1/core/router/app_route.dart';
import 'package:niogu_ecommerce_v1/core/utils/extenstion_format.dart';
import 'package:niogu_ecommerce_v1/core/widgets/custom_snackbar.dart';
import 'package:niogu_ecommerce_v1/features/order/presentation/providers/order_provider.dart';
import 'package:sizer/sizer.dart';
import 'package:niogu_ecommerce_v1/core/constant/app_color.dart';

class CustomActionScreen extends ConsumerWidget {
  final String orderId;
  final String orderNumber;
  final ActionType type;

  const CustomActionScreen({
    super.key,
    required this.orderId,
    required this.orderNumber,
    required this.type,
  });

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    String title;
    String description;
    IconData icon;
    Color themeColor;

    switch (type) {
      case ActionType.checkout:
        title = "Pesanan Berhasil!";
        description =
            "Terima kasih! Pesanan anda sedang kami siapkan dan akan segera dikirim.";
        icon = Icons.check_circle_outline;
        themeColor = AppColor.primaryColor;
        break;
      case ActionType.cancel:
        title = "Pesanan Dibatalkan";
        description = "Pesanan anda telah berhasil dibatalkan";
        icon = Icons.cancel_outlined;
        themeColor = Colors.red;
        break;
      case ActionType.requestCancel:
        title = "Pengajuan Terkirim";
        description =
            "Permintaan pembatalan anda telah diterima dan sedang menunggu persetujuan admin.";
        icon = Icons.access_time_outlined;
        themeColor = Colors.orange;
        break;
    }

    return SafeArea(
      top: false,
      bottom: true,
      right: false,
      left: false,
      child: Scaffold(
        backgroundColor: Colors.white,
        body: Container(
          width: double.infinity,
          padding: EdgeInsets.symmetric(horizontal: 8.w),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              Container(
                padding: EdgeInsets.all(6.w),
                decoration: BoxDecoration(
                  color: themeColor.withOpacity(0.1),
                  shape: BoxShape.circle,
                ),
                child: Icon(icon, size: 25.w, color: themeColor),
              ),
              SizedBox(height: 5.h),

              Text(
                title,
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: AppFontSize.medium.sp,
                  fontWeight: FontWeight.bold,
                  color: Colors.black87,
                ),
              ),
              SizedBox(height: 2.h),

              Container(
                padding: EdgeInsets.symmetric(horizontal: 4.w, vertical: 1.h),
                decoration: BoxDecoration(
                  color: Colors.grey.shade100,
                  borderRadius: BorderRadius.circular(2.5.w),
                ),
                child: Text(
                  "Nomor Pesanan : ${orderNumber.toUniqueNumberCleaner()}",
                  style: TextStyle(
                    fontSize: AppFontSize.small.sp,
                    fontWeight: FontWeight.w600,
                    color: Colors.grey.shade700,
                  ),
                ),
              ),
              SizedBox(height: 3.h),

              Text(
                description,
                textAlign: TextAlign.center,
                style: TextStyle(
                  fontSize: AppFontSize.small.sp,
                  color: Colors.grey.shade600,
                  height: 1.5,
                ),
              ),
              SizedBox(height: 8.h),

              SizedBox(
                width: double.infinity,
                child: ElevatedButton(
                  onPressed: () {
                    context.goNamed(AppRoute.homeScreen);
                  },
                  style: ElevatedButton.styleFrom(
                    backgroundColor: AppColor.primaryColor,
                    padding: EdgeInsets.symmetric(vertical: 2.h),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(2.5.w),
                    ),
                    elevation: 0,
                  ),
                  child: Text(
                    "Kembali ke Beranda",
                    style: TextStyle(
                      fontSize: AppFontSize.medium.sp,
                      fontWeight: FontWeight.bold,
                      color: Colors.white,
                    ),
                  ),
                ),
              ),
              SizedBox(height: 2.h),

              if (type == ActionType.checkout)
                TextButton(
                  onPressed: () async {
                    final orderInfo = await ref
                        .read(orderRepositoryProvider)
                        .fetchOrderById(orderId);

                    if (orderInfo == null) {
                      CustomSnackbar.showError(
                        context,
                        "Ups, terjadi kesalahan",
                      );
                      return;
                    }

                    context.goNamed(
                      AppRoute.orderDetailScreen,
                      extra: orderInfo,
                    );
                  },
                  child: Text(
                    "Lihat Detail Pesanan",
                    style: TextStyle(
                      fontSize: AppFontSize.medium.sp,
                      color: AppColor.primaryColor,
                      fontWeight: FontWeight.w600,
                    ),
                  ),
                ),
            ],
          ),
        ),
      ),
    );
  }
}
