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/router/app_route.dart';
import 'package:niogu_app/core/enums/item_type.dart';
import 'package:niogu_app/core/utils/log_message.dart';
import 'package:niogu_app/core/enums/stock_card_type.dart';
import 'package:niogu_app/core/enums/stock_type.dart';
import 'package:niogu_app/core/system/system_setting.dart';
import 'package:niogu_app/core/widgets/custom_selector.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/goods/domain/entities/stock_card.dart';
import 'package:niogu_app/features/goods/presentation/widgets/dropdown_with_action.dart';
import 'package:niogu_app/features/goods/raw_materials/domain/entities/raw_material.dart';
import 'package:niogu_app/features/goods/raw_materials/presentation/providers/raw_material_provider.dart';
import 'package:niogu_app/features/goods/units/domain/entities/unit.dart';
import 'package:niogu_app/features/goods/units/presentation/providers/unit_provider.dart';
import 'package:niogu_app/features/goods/units/presentation/screens/add_unit_screen.dart';
import 'package:niogu_app/features/goods/units/presentation/screens/edit_unit_screen.dart';
import 'package:niogu_app/features/goods/domain/entities/outlet_inventory.dart';
import 'package:niogu_app/features/outlets/presentation/providers/outlet_provider.dart';
import 'package:sizer/sizer.dart';

class AddRawMaterialScreen extends ConsumerStatefulWidget {
  const AddRawMaterialScreen({super.key});

  @override
  ConsumerState<AddRawMaterialScreen> createState() =>
      _AddRawMaterialScreenState();
}

class _AddRawMaterialScreenState extends ConsumerState<AddRawMaterialScreen> {
  final GlobalKey<FormState> _formKey = GlobalKey<FormState>();

  final TextEditingController _nameController = TextEditingController();

  final TextEditingController _purchasePriceController =
      TextEditingController();

  final TextEditingController _stockController = TextEditingController();

  String? _selectedUnit;

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

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

  String? _validator({
    String? value,
    required String message1,
    String? message2,
    bool isNumber = false,
    bool isPurchasePrice = false,
    bool isStock = false,
    String? errorMessage,
  }) {
    if (value == null || value.isEmpty) {
      return message1;
    }

    try {
      if (isNumber && double.parse(value) <= 0) {
        if (isPurchasePrice) return message2;

        if (isStock) return message2;
      }
    } catch (e) {
      return errorMessage;
    }

    return null;
  }

  Future<void> _saveRawMaterial() async {
    if (!_formKey.currentState!.validate()) {
      return;
    }

    if (_selectedUnit == null) {
      CustomSnackbar.showError(context, "Satuan bahan baku belum dipilih");
      return;
    }

    final String name = _nameController.text.trim();
    final String unitId = _selectedUnit!;
    final rawMaterial = UpsertRawMaterial(unitId: unitId, name: name);

    final selectProductUsageMaterials = ref
        .read(selectedProductUsageMaterialProvider)
        .values
        .toList();

    final List<UpsertReceiptItem> receiptItems = selectProductUsageMaterials
        .map((selectProductUsage) {
          return UpsertReceiptItem(
            productUsageId: selectProductUsage.productUsageId,
            rawMaterialId: rawMaterial.localId,
            quantity: selectProductUsage.quantity,
          );
        })
        .toList();

    final userId = await SystemSetting.getCurrentUserId();

    final String? outletId = await SystemSetting.getCurrentOutletId();

    try {
      final double stock = double.parse(_stockController.text.trim());

      final double purchasePrice = double.parse(
        _purchasePriceController.text.trim(),
      );

      final List<UpsertOutletInventory> outletInventories = [
        UpsertOutletInventory(
          outletId: outletId!,
          itemType: ItemType.raw_material,
          itemId: rawMaterial.localId,
          stockType: StockType.fixed,
          stock: stock,
          costPrice: 0.0,
          sellingPrice: 0.0,
          purchasePrice: purchasePrice,
        ),
      ];

      final List<StockCard> stockCards = [
        StockCard(
          outletInventoryId: outletInventories.single.localId,
          userId: userId!,
          stockCardType: StockCardType.initial_stock,
          qtyChange: stock,
          currentStock: stock,
          costPrice: purchasePrice,
          stockType: StockType.fixed,
        ),
      ];

      final selectProductUsage = ref.read(selectedProductUsageMaterialProvider);

      if (selectProductUsage.values.isEmpty) {
        CustomSnackbar.showError(context, "Penggunaan bahan baku belum diatur");
        return;
      }

      try {
        await ref
            .read(rawMaterialControllerProvider.notifier)
            .saveRawMaterial(
              rawMaterial: rawMaterial,
              receiptItems: receiptItems,
              outletInventories: outletInventories,
              stockCards: stockCards,
            );

        if (!mounted) return;

        CustomSnackbar.showSuccess(context, "Bahan baku berhasil disimpan");

        context.pop();
      } catch (e, st) {
        LogMessage.log.e(e.toString(), error: e, stackTrace: st);

        CustomSnackbar.showError(context, "Ups, terjadi kesalahan");
      }
    } catch (e) {
      CustomSnackbar.showError(context, "Format angka tidak valid");
    }
  }

  @override
  Widget build(BuildContext context) {
    return LayoutBuilder(
      builder: (context, constraints) {
        final bool isTablet = 100.w >= 600;

        final List<Unit> units = [];

        final unitState = ref.watch(unitStreamProvider);

        unitState.whenData((u) => units.addAll(u));

        final selectProductUsageMaterialState = ref.watch(
          selectedProductUsageMaterialProvider,
        );

        final rawMaterialControllerState = ref.watch(
          rawMaterialControllerProvider,
        );

        return PopScope(
          canPop: false,
          onPopInvokedWithResult: (didPop, result) async {
            if (didPop) return;

            context.pop();
          },
          child: SafeArea(
            top: false,
            bottom: true,
            right: false,
            left: false,
            child:                 Scaffold(
                  backgroundColor: const Color(0xFFF9FAFB),
                  appBar: TopBackBarApp(
                    title: "Tambah Bahan Baku",
                    onTap: () => context.pop(),
                  ),

                  body: SingleChildScrollView(
                    padding: EdgeInsets.symmetric(
                      horizontal: 5.w,
                      vertical: 3.h,
                    ),
                    child: Form(
                      key: _formKey,
                      child: Column(
                        children: [
                          CustomTextFormField(
                            label: "Nama Bahan Baku",
                            hint: "Contoh: Kopi Susu Gula Aren",
                            controller: _nameController,
                            validator: (value) => _validator(
                              value: value,
                              message1: "Nama produk belum diisi",
                            ),
                          ),

                          SizedBox(height: 3.h),

                          CustomTextFormField(
                            label: "Harga Beli",
                            hint: "0",
                            keyboardType: TextInputType.number,
                            controller: _purchasePriceController,
                            validator: (value) => _validator(
                              value: value,
                              message1: "Harga beli belum diisi",
                              message2: "Harga beli harus lebih dari 0",
                              isNumber: true,
                              isPurchasePrice: true,
                              errorMessage: "Harga beli tidak valid",
                            ),
                          ),

                          SizedBox(height: 3.h),

                          CustomTextFormField(
                            label: "Stok Awal",
                            hint: "100",
                            keyboardType: TextInputType.number,
                            controller: _stockController,
                            validator: (value) => _validator(
                              value: value,
                              message1: "Stok belum diisi",
                              message2: "Stok harus lebih dari 0",
                              isNumber: true,
                              isStock: true,
                              errorMessage: "Stok tidak valid",
                            ),
                          ),

                          SizedBox(height: 3.h),

                          DropdownWithAction(
                            label: "Satuan",
                            value: _selectedUnit,
                            items: units,
                            onChanged: (val) =>
                                setState(() => _selectedUnit = val),
                            onAdd: () async {
                              FocusScope.of(context).unfocus();

                              await Future.delayed(
                                const Duration(milliseconds: 400),
                              );

                              showDialog(
                                context: context,
                                builder: (_) => const AddUnitScreen(),
                              );
                            },
                            onEdit: () async {
                              if (_selectedUnit == null) {
                                CustomSnackbar.showError(
                                  context,
                                  "Satuan belum dipilih",
                                );
                              } else {
                                final unitRepository = ref.read(
                                  unitRepositoryProvider,
                                );

                                final unit = await unitRepository.fetchUnitById(
                                  _selectedUnit!,
                                );

                                if (unit != null) {
                                  showDialog(
                                    context: context,
                                    builder: (_) {
                                      return EditUnitScreen(unit: unit);
                                    },
                                  );
                                } else {
                                  CustomSnackbar.showError(
                                    context,
                                    "Satuan produk tidak tersedia",
                                  );
                                }
                              }
                            },
                          ),

                          SizedBox(height: 3.h),

                          CustomSelector(
                            isTablet: isTablet,
                            title:
                                selectProductUsageMaterialState.values.isEmpty
                                ? "Atur penggunaan bahan baku"
                                : "Lihat ${selectProductUsageMaterialState.values.length} penggunaan bahan baku",
                            padding: EdgeInsets.symmetric(
                              horizontal: 4.w,
                              vertical: 2.h,
                            ),
                            fontSize: isTablet
                                ? AppFontSize.medium.sp
                                : AppFontSize.small.sp,
                            onTap: () async {
                              FocusScope.of(context).unfocus();

                              await Future.delayed(
                                const Duration(milliseconds: 400),
                              );

                              final isMainOutlet = await ref
                                  .read(outletRepositoryProvider)
                                  .fetchIsMainOutlet();

                              if (_selectedUnit == null) {
                                CustomSnackbar.showError(
                                  context,
                                  "Satuan belum dipilih",
                                );
                              } else {
                                final unitRepository = ref.read(
                                  unitRepositoryProvider,
                                );

                                final unit = await unitRepository.fetchUnitById(
                                  _selectedUnit!,
                                );

                                if (unit != null) {
                                  context.pushNamed(
                                    AppRoute.productUsageRawMaterialScreen,
                                    extra: {
                                      "unit-name": unit.name,
                                      "is_main_outlet": isMainOutlet,
                                      "is_editing": true
                                    },
                                  );
                                } else {
                                  CustomSnackbar.showError(
                                    context,
                                    "Satuan produk tidak tersedia",
                                  );
                                }
                              }
                            },
                          ),

                          SizedBox(height: 3.h),

                          SizedBox(
                            width: double.infinity,
                            height: 6.5.h,
                            child: ElevatedButton(
                              onPressed: rawMaterialControllerState.isLoading
                                  ? null
                                  : _saveRawMaterial,
                              style: ElevatedButton.styleFrom(
                                backgroundColor: AppColor.primaryColor,
                                shape: RoundedRectangleBorder(
                                  borderRadius: BorderRadius.circular(2.5.w),
                                ),
                                disabledBackgroundColor: Colors.grey.shade300,
                                elevation: 4,
                              ),
                              child: Text(
                                "Simpan",
                                style: TextStyle(
                                  color: Colors.white,
                                  fontSize: AppFontSize.medium.sp,
                                  fontWeight: FontWeight.bold,
                                ),
                              ),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
          ),
        );
      },
    );
  }
}
