import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:go_router/go_router.dart';
import 'package:niogu_app/core/components/top_back_bar_app.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/core/widgets/custom_text_form_field.dart';
import 'package:niogu_app/features/supplier/domain/entities/supplier.dart';
import 'package:niogu_app/features/supplier/presentation/providers/supplier_provider.dart';
import 'package:sizer/sizer.dart';

class AddSupplierScreen extends ConsumerStatefulWidget {
  final String? initialName;
  final String? initialPhone;
  const AddSupplierScreen({super.key, this.initialName, this.initialPhone});

  @override
  ConsumerState<AddSupplierScreen> createState() => _AddSupplierScreenState();
}

class _AddSupplierScreenState extends ConsumerState<AddSupplierScreen> {
  final GlobalKey<FormState> _formKey = GlobalKey();

  final TextEditingController _nameController = TextEditingController();

  final TextEditingController _emailController = TextEditingController();

  final TextEditingController _phoneController = TextEditingController();

  @override
  void initState() {
    super.initState();
    _nameController.text = widget.initialName ?? '';
    _phoneController.text = widget.initialPhone ?? '';
  }

  @override
  void dispose() {
    _nameController.dispose();
    _emailController.dispose();
    _phoneController.dispose();
    super.dispose();
  }

  Future<void> _addSupplier() async {
    final String name = _nameController.text;

    final String? email = _emailController.text.isNotEmpty
        ? _emailController.text
        : null;

    final String? phoneNumber = _phoneController.text.isNotEmpty
        ? _phoneController.text
        : null;

    final supplier = UpsertSupplier(
      name: name,
      email: email,
      phoneNumber: phoneNumber,
    );

    try {
      await ref
          .read(supplierControllerProvider.notifier)
          .saveSupplier(supplier);

      if (!mounted) return;

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

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints) {
        final supplierControllerState = ref.watch(supplierControllerProvider);

        return SafeArea(
          top: false,
          bottom: true,
          right: false,
          left: false,
          child: Scaffold(
            backgroundColor: Colors.white,
            appBar: TopBackBarApp(
              title: "Tambah Pemasok",
              onTap: () => context.pop(),
            ),
            body: Column(
              children: [
                Expanded(
                  child: SingleChildScrollView(
                    padding: EdgeInsets.symmetric(
                      horizontal: 5.w,
                      vertical: 2.h,
                    ),
                    child: Form(
                      key: _formKey,
                      child: Column(
                        children: [
                          Center(
                            child: Container(
                              width: 25.w,
                              height: 25.w,
                              decoration: BoxDecoration(
                                color: Colors.grey[100],
                                shape: BoxShape.circle,
                                border: Border.all(
                                  color: Colors.grey.shade200,
                                  width: 2,
                                ),
                              ),
                              child: Icon(
                                Icons.person_rounded,
                                size: 12.w,
                                color: Colors.grey[400],
                              ),
                            ),
                          ),

                          SizedBox(height: 4.h),

                          CustomTextFormField(
                            label: "Nama Pemasok",
                            controller: _nameController,
                            hint: "Masukkan nama",
                            validator: (value) {
                              if (value == null || value.isEmpty) {
                                return "Nama pemasok belum diisi";
                              }

                              return null;
                            },
                            prefixIcon: Icons.person_outline,
                          ),

                          SizedBox(height: 2.h),
                          CustomTextFormField(
                            label: "Email",
                            controller: _emailController,
                            hint: "Masukkan email (opsional)",
                            keyboardType: TextInputType.emailAddress,
                            prefixIcon: Icons.email_outlined,
                          ),
                          SizedBox(height: 2.h),
                          CustomTextFormField(
                            label: "No. Handphone / WA",
                            controller: _phoneController,
                            hint: "Masukkan no handphone / wa (opsional)",
                            keyboardType: TextInputType.phone,
                            prefixIcon: Icons.phone_android_outlined,
                          ),
                        ],
                      ),
                    ),
                  ),
                ),

                Container(
                  padding: EdgeInsets.all(5.w),
                  decoration: BoxDecoration(
                    color: Colors.white,
                    boxShadow: [
                      BoxShadow(
                        color: Colors.black.withOpacity(0.05),
                        blurRadius: 10,
                        offset: const Offset(0, -5),
                      ),
                    ],
                  ),
                  child: SizedBox(
                    width: double.infinity,
                    height: 6.5.h,
                    child: ElevatedButton(
                      onPressed: supplierControllerState.isLoading
                          ? null
                          : _addSupplier,
                      style: ElevatedButton.styleFrom(
                        backgroundColor: AppColor.primaryColor,
                        shape: RoundedRectangleBorder(
                          borderRadius: BorderRadius.circular(2.5.w),
                        ),
                        disabledBackgroundColor: Colors.grey.shade300,
                      ),
                      child: Text(
                        "Simpan Pemasok",
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: AppFontSize.medium.sp,
                          fontWeight: FontWeight.bold,
                        ),
                      ),
                    ),
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
