import 'package:didvan/models/overview_data.dart';
import 'package:didvan/views/widgets/didvan/text.dart';
import 'package:didvan/views/widgets/skeleton_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_svg/svg.dart';
import 'package:persian_number_utility/persian_number_utility.dart';

class PodcastListCard extends StatelessWidget {
  final OverviewData podcast;
  final VoidCallback onTap;

  const PodcastListCard({
    super.key,
    required this.podcast,
    required this.onTap,
  });

  String _formatDuration(int? duration) {
    if (duration == null) return '';
    final minutes = duration ~/ 60;
    final seconds = duration % 60;
    return '”${seconds.toString().padLeft(2, '0')}:’${minutes.toString().padLeft(2, '0')}';
  }

  String _formatDate(String dateStr) {
    try {
      final date = DateTime.parse(dateStr);
      return date.toPersianDateStr();
    } catch (e) {
      return dateStr;
    }
  }

  @override
  Widget build(BuildContext context) {
    final textTheme = Theme.of(context).textTheme;

    return GestureDetector(
      onTap: onTap,
      child: Column(
        children: [
          Container(
            height: 120,
            margin: const EdgeInsets.only(
              bottom: 8,
              left: 16,
              right: 16,
            ),
            decoration: BoxDecoration(
              borderRadius: BorderRadius.circular(20),
            ),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(20),
              child: Row(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Padding(
                    padding: const EdgeInsets.all(6.0),
                    child: AspectRatio(
                      aspectRatio: 1.42,
                      child: ClipRRect(
                        borderRadius: BorderRadius.circular(16),
                        child: SkeletonImage(
                          imageUrl: podcast.image,
                          width: double.infinity,
                          height: double.infinity,
                          borderRadius: BorderRadius.circular(16),
                        ),
                      ),
                    ),
                  ),
                  Expanded(
                    child: Padding(
                      padding: const EdgeInsets.fromLTRB(12, 3, 12, 10),
                      child: Container(
                        color: Colors.white,
                        child: Column(
                          crossAxisAlignment: CrossAxisAlignment.start,
                          mainAxisAlignment: MainAxisAlignment.start,
                          children: [
                            DidvanText(
                              podcast.title,
                              style: textTheme.bodyMedium?.copyWith(
                                fontWeight: FontWeight.w600,
                                color: Colors.black87,
                              ),
                              maxLines: 2,
                              overflow: TextOverflow.ellipsis,
                            ),
                            const Spacer(),
                            Row(
                              children: [
                                SvgPicture.asset(
                                    'lib/assets/icons/calendar.svg'),
                                const SizedBox(width: 4),
                                DidvanText(
                                  _formatDate(podcast.createdAt),
                                  style: textTheme.bodySmall?.copyWith(
                                    color: const Color.fromARGB(
                                        255, 102, 102, 102),
                                  ),
                                  maxLines: 1,
                                  overflow: TextOverflow.ellipsis,
                                ),
                              ],
                            ),
                            const SizedBox(height: 7),
                            Row(
                              children: [
                                SvgPicture.asset(
                                  'lib/assets/icons/clock.svg',
                                  color:
                                      const Color.fromARGB(255, 102, 102, 102),
                                ),
                                const SizedBox(width: 4),
                                DidvanText(
                                  _formatDuration(podcast.duration)
                                      .toPersianDigit(),
                                  style: textTheme.bodySmall?.copyWith(
                                    color: const Color.fromARGB(
                                        255, 102, 102, 102),
                                  ),
                                ),
                              ],
                            ),
                          ],
                        ),
                      ),
                    ),
                  ),
                ],
              ),
            ),
          ),
          Divider(
            color: Colors.grey[300],
            thickness: 2,
            indent: 16,
            endIndent: 16,
          ),
        ],
      ),
    );
  }
}
