import 'dart:io';

import 'package:flutter/material.dart';
import 'package:sizer/sizer.dart';

class ProductThumbnail extends StatelessWidget {
  final String? imagePath;
  const ProductThumbnail({super.key, required this.imagePath});

  @override
  Widget build(BuildContext context) {
    final bool imagePathAvailable = imagePath != null && imagePath!.isNotEmpty;

    final File imageFile = File(
      imagePathAvailable ? imagePath! : "image not found",
    );

    final bool imageFileExists = imageFile.existsSync();

    return RepaintBoundary(
      child: Container(
        width: 18.w,
        height: 18.w,
        decoration: BoxDecoration(
          color: Colors.white,
          borderRadius: BorderRadius.circular(3.w),
          border: Border.all(color: Colors.grey.shade200),
        ),
        child: imageFileExists
            ? ClipRRect(
                borderRadius: BorderRadius.circular(2.5.w),
                child: Image.file(
                  imageFile,
                  fit: BoxFit.cover,
                  width: double.infinity,
                  height: double.infinity,
                  errorBuilder: (context, error, stackTrace) {
                    return Icon(
                      Icons.error,
                      color: Colors.grey[400],
                      size: 8.w,
                    );
                  },
                ),
              )
            : Icon(Icons.image_outlined, size: 8.w, color: Colors.grey[400]),
      ),
    );
  }
}
