import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:niogu_app/core/constants/app_color.dart';
import 'package:niogu_app/core/constants/app_font_size.dart';
import 'package:niogu_app/core/utils/log_message.dart';
import 'package:niogu_app/core/widgets/custom_snackbar.dart';
import 'package:niogu_app/features/outlets/presentation/providers/outlet_provider.dart';
import 'package:sizer/sizer.dart';

class DeleteStaffAdminScreen extends ConsumerStatefulWidget {
  final String id;
  final String name;
  final int transactionAmount;
  const DeleteStaffAdminScreen({
    super.key,
    required this.id,
    required this.name,
    required this.transactionAmount,
  });

  @override
  ConsumerState<DeleteStaffAdminScreen> createState() =>
      _DeleteStaffAdminScreenState();
}

class _DeleteStaffAdminScreenState
    extends ConsumerState<DeleteStaffAdminScreen> {
  late String name;

  late int transactionAmount;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    name = widget.name;

    transactionAmount = widget.transactionAmount;
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  Future<void> _onDeleteStaff() async {
    try {
      await ref
          .read(outletControllerProvider.notifier)
          .deleteStaffAdmin(widget.id);

      if (!mounted) return;

      CustomSnackbar.showSuccess(context, 'Staf berhasil dihapus');
      context.pop();
      context.pop();
    } catch (e, st) {
      LogMessage.log.e(e.toString(), error: e, stackTrace: st);
      CustomSnackbar.showError(context, "Ups, terjadi kesalahan");
      context.pop();
      context.pop();
    }
  }

  @override
  Widget build(BuildContext context) {
    final bool isTablet = 100.w >= 600;

    final double maxWidth = 100.w >= 1280
        ? 1200
        : isTablet
        ? 800
        : 400;

    final outletControllerState = ref.watch(outletControllerProvider);

    return Dialog(
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(4.w)),
      elevation: 0,
      backgroundColor: Colors.transparent,
      child: Container(
        padding: EdgeInsets.all(5.w),
        width: double.infinity,
        constraints: BoxConstraints(maxWidth: maxWidth),
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(4.w),
        ),
        child: Column(
          mainAxisSize: MainAxisSize.min,
          children: [
            Container(
              padding: EdgeInsets.all(3.w),
              decoration: BoxDecoration(
                color: Colors.red[50],
                shape: BoxShape.circle,
              ),
              child: Icon(Icons.delete_outline, color: Colors.red, size: 8.w),
            ),
            SizedBox(height: 2.h),
            Text(
              "Hapus Staf?",
              style: TextStyle(
                fontSize: AppFontSize.medium.sp,
                fontWeight: FontWeight.bold,
                color: Colors.black87,
              ),
            ),
            SizedBox(height: 1.h),
            Text(
              "Tindakan ini tidak dapat dibatalkan.",
              style: TextStyle(
                fontSize: isTablet
                    ? AppFontSize.medium.sp
                    : AppFontSize.small.sp,
                color: Colors.grey[800],
              ),
            ),
            SizedBox(height: 3.h),

            Container(
              padding: EdgeInsets.symmetric(vertical: 3.w),
              decoration: BoxDecoration(
                borderRadius: BorderRadius.circular(2.5.w),
              ),
              child: Row(
                children: [
                  Container(
                    height: 10.w,
                    width: 10.w,
                    decoration: BoxDecoration(
                      color: Colors.blue[50],
                      borderRadius: BorderRadius.circular(2.5.w),
                    ),
                    child: Icon(
                      Icons.person_outline,
                      color: AppColor.primaryColor,
                      size: 5.w,
                    ),
                  ),

                  SizedBox(width: 3.w),

                  // Detail Text
                  Expanded(
                    child: Column(
                      crossAxisAlignment: CrossAxisAlignment.start,
                      children: [
                        Text(
                          name,
                          maxLines: 1,
                          overflow: TextOverflow.ellipsis,
                          style: TextStyle(
                            fontSize: isTablet
                                ? AppFontSize.medium.sp
                                : AppFontSize.small.sp,
                            fontWeight: FontWeight.bold,
                            color: Colors.black87,
                          ),
                        ),
                        SizedBox(height: 0.5.h),
                        // Badge Terjual
                        Container(
                          padding: EdgeInsets.symmetric(
                            horizontal: 2.w,
                            vertical: 0.5.h,
                          ),
                          decoration: BoxDecoration(
                            color: Colors.green[50],
                            borderRadius: BorderRadius.circular(1.w),
                          ),
                          child: Text(
                            "$transactionAmount Transaksi",
                            style: TextStyle(
                              fontSize: isTablet
                                  ? (AppFontSize.medium - 2).sp
                                  : (AppFontSize.small - 2).sp,
                              color: Colors.green[700],
                              fontWeight: FontWeight.w600,
                            ),
                          ),
                        ),
                      ],
                    ),
                  ),
                ],
              ),
            ),

            SizedBox(height: 3.h),

            _buildButton(
              context,
              label: "Hapus",
              color: Colors.red,
              onPressed: outletControllerState.isLoading
                  ? null
                  : _onDeleteStaff,
            ),

            SizedBox(height: 1.5.h),

            TextButton(
              onPressed: outletControllerState.isLoading
                  ? null
                  : () => context.pop(),
              child: Text(
                "Batal",
                style: TextStyle(
                  color: Colors.grey[600],
                  fontSize: AppFontSize.medium.sp,
                  fontWeight: FontWeight.w600,
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }

  Widget _buildButton(
    BuildContext context, {
    required String label,
    required Color color,
    required VoidCallback? onPressed,
  }) {
    return SizedBox(
      width: double.infinity,
      height: 6.h,
      child: ElevatedButton(
        onPressed: onPressed,
        style: ElevatedButton.styleFrom(
          backgroundColor: color,
          elevation: 0,
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(2.5.w),
          ),
          disabledBackgroundColor: Colors.grey.shade300,
        ),
        child: Text(
          label,
          style: TextStyle(
            color: Colors.white,
            fontSize: AppFontSize.medium.sp,
            fontWeight: FontWeight.bold,
          ),
        ),
      ),
    );
  }
}
