import 'package:cached_network_image/cached_network_image.dart';
import 'package:enaklo_pos/core/components/custom_modal_dialog.dart';
import 'package:enaklo_pos/core/components/spaces.dart';
import 'package:enaklo_pos/core/constants/colors.dart';
import 'package:enaklo_pos/core/constants/variables.dart';
import 'package:enaklo_pos/core/extensions/build_context_ext.dart';
import 'package:enaklo_pos/core/extensions/string_ext.dart';
import 'package:enaklo_pos/data/models/response/product_response_model.dart';
import 'package:flutter/material.dart';

class DetailProductDialog extends StatelessWidget {
  final Product product;
  const DetailProductDialog({super.key, required this.product});

  @override
  Widget build(BuildContext context) {
    return CustomModalDialog(
      title: "Detail Produk",
      maxWidth: context.deviceWidth * 0.5,
      child: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Row(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                ClipRRect(
                  borderRadius: BorderRadius.circular(12),
                  child: CachedNetworkImage(
                    imageUrl: product.image!.contains('http')
                        ? product.image!
                        : '${Variables.baseUrl}/${product.image}',
                    fit: BoxFit.cover,
                    width: 120,
                    height: 120,
                    errorWidget: (context, url, error) => Container(
                      width: 120,
                      height: 120,
                      decoration: BoxDecoration(
                        color: AppColors.grey.withOpacity(0.1),
                        borderRadius: BorderRadius.circular(12),
                      ),
                      child: const Icon(
                        Icons.image_outlined,
                        color: AppColors.grey,
                        size: 40,
                      ),
                    ),
                  ),
                ),
                const SpaceWidth(16),
                Expanded(
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Align(
                        alignment: Alignment.centerRight,
                        child: _buildStatus(),
                      ),
                      const SpaceHeight(8),
                      Text(
                        product.name ?? "-",
                        style: const TextStyle(
                          fontSize: 16,
                          fontWeight: FontWeight.w600,
                        ),
                      ),
                      if (product.description != null &&
                          product.description!.isNotEmpty)
                        Padding(
                          padding: const EdgeInsets.only(top: 4.0),
                          child: Text(
                            product.description!,
                            style: const TextStyle(
                              fontSize: 14,
                              color: AppColors.grey,
                            ),
                          ),
                        ),
                    ],
                  ),
                ),
              ],
            ),
            const SpaceHeight(16),
            Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                _buildItem(
                  product.category?.name ?? "-",
                  "Kategori",
                ),
                _buildItem(
                  "${product.stock}",
                  "Stok",
                  valueColor: product.stock! < 50
                      ? AppColors.red
                      : product.stock! < 100
                          ? Colors.yellow
                          : AppColors.green,
                ),
                _buildItem(
                  (product.price ?? "0").currencyFormatRpV2,
                  "Harga",
                  valueColor: AppColors.primary,
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }

  Column _buildItem(String value, String label,
      {Color valueColor = AppColors.black}) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(
          label,
          style: const TextStyle(
            fontSize: 12,
            color: Colors.grey,
          ),
        ),
        const SpaceHeight(4),
        Text(
          value,
          style: TextStyle(
            fontSize: 14,
            color: valueColor,
            fontWeight: FontWeight.w600,
          ),
        ),
      ],
    );
  }

  Container _buildStatus() {
    return Container(
      padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
      decoration: BoxDecoration(
        color: product.status == 1 ? AppColors.green : AppColors.red,
        borderRadius: BorderRadius.circular(8),
      ),
      child: Text(
        product.status == 1 ? 'Aktif' : 'Tidak Aktif',
        style: const TextStyle(
            color: Colors.white, fontSize: 12, fontWeight: FontWeight.w700),
      ),
    );
  }
}
