import 'package:flutter/material.dart';

const marketingGreen = Color(0xFF026930);
const marketingGreenDark = Color(0xFF034D26);
const marketingInk = Color(0xFF24332B);
const marketingMuted = Color(0xFF68776E);
const marketingLine = Color(0xFFDDE4DF);
const marketingCanvas = Color(0xFFF5F7F6);
const marketingBlue = Color(0xFF3578B8);

int marketingNumber(dynamic value) => int.tryParse('$value') ?? 0;
String marketingText(dynamic value, [String fallback = '—']) =>
    value == null || value.toString().trim().isEmpty ? fallback : value.toString();

// Converts API JSON list values into rows the Flutter pages can use safely.
List<Map<String, dynamic>> marketingMaps(dynamic value) => value is List
    ? value.whereType<Map>().map((row) => Map<String, dynamic>.from(row)).toList()
    : <Map<String, dynamic>>[];

class MarketingPageHeading extends StatelessWidget {
  const MarketingPageHeading({
    super.key,
    required this.title,
    required this.subtitle,
    required this.icon,
    this.actionText,
    this.onAction,
  });

  final String title;
  final String subtitle;
  final IconData icon;
  final String? actionText;
  final VoidCallback? onAction;

  @override
  Widget build(BuildContext context) {
    final width = MediaQuery.sizeOf(context).width;
    return Wrap(
      alignment: WrapAlignment.spaceBetween,
      crossAxisAlignment: WrapCrossAlignment.center,
      runSpacing: 10,
      spacing: 12,
      children: [
        SizedBox(
          width: width < 520 ? width - 44 : 390,
          child: Row(children: [
            Container(
              width: 42,
              height: 42,
              color: const Color(0xFFEAF3EE),
              child: Icon(icon, color: marketingGreen),
            ),
            const SizedBox(width: 12),
            Expanded(child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(title, maxLines: 1, overflow: TextOverflow.ellipsis,
                    style: const TextStyle(color: marketingGreen, fontSize: 19, fontWeight: FontWeight.w900)),
                Text(subtitle, maxLines: 2, overflow: TextOverflow.ellipsis,
                    style: const TextStyle(color: marketingMuted, fontSize: 12)),
              ],
            )),
          ]),
        ),
        if (actionText != null && onAction != null)
          ElevatedButton.icon(
            onPressed: onAction,
            icon: const Icon(Icons.add, size: 18),
            label: Text(actionText!),
            style: ElevatedButton.styleFrom(
              backgroundColor: marketingGreen,
              foregroundColor: Colors.white,
              elevation: 0,
              padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 14),
              shape: const RoundedRectangleBorder(borderRadius: BorderRadius.zero),
            ),
          ),
      ],
    );
  }
}

class MarketingMetricCard extends StatelessWidget {
  const MarketingMetricCard({super.key, required this.title, required this.value, required this.icon, this.color = marketingGreen});
  final String title;
  final String value;
  final IconData icon;
  final Color color;

  @override
  Widget build(BuildContext context) => Container(
    constraints: const BoxConstraints(minHeight: 82),
    padding: const EdgeInsets.all(13),
    decoration: BoxDecoration(color: Colors.white, border: Border.all(color: marketingLine)),
    child: Row(children: [
      Container(width: 40, height: 40, color: color.withOpacity(.09), child: Icon(icon, color: color, size: 21)),
      const SizedBox(width: 11),
      Expanded(child: Column(crossAxisAlignment: CrossAxisAlignment.start, mainAxisAlignment: MainAxisAlignment.center, children: [
        Text(title, maxLines: 1, overflow: TextOverflow.ellipsis, style: const TextStyle(color: marketingMuted, fontSize: 12)),
        const SizedBox(height: 3),
        Text(value, maxLines: 1, overflow: TextOverflow.ellipsis, style: const TextStyle(color: marketingInk, fontSize: 19, fontWeight: FontWeight.w800)),
      ])),
    ]),
  );
}

class MarketingRecordTile extends StatelessWidget {
  const MarketingRecordTile({super.key, required this.icon, required this.title, required this.detail, this.trailing});
  final IconData icon;
  final String title;
  final String detail;
  final String? trailing;

  @override
  Widget build(BuildContext context) => Container(
    padding: const EdgeInsets.symmetric(horizontal: 14, vertical: 12),
    decoration: BoxDecoration(color: Colors.white, border: Border.all(color: marketingLine)),
    child: Row(children: [
      Container(width: 40, height: 40, color: const Color(0xFFEAF3EE), child: Icon(icon, color: marketingGreen, size: 20)),
      const SizedBox(width: 12),
      Expanded(child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
        Text(title, maxLines: 1, overflow: TextOverflow.ellipsis, style: const TextStyle(color: marketingInk, fontWeight: FontWeight.w800)),
        const SizedBox(height: 3),
        Text(detail, maxLines: 1, overflow: TextOverflow.ellipsis, style: const TextStyle(color: marketingMuted, fontSize: 12)),
      ])),
      if (trailing != null) ...[
        const SizedBox(width: 8),
        ConstrainedBox(constraints: const BoxConstraints(maxWidth: 130), child: Text(trailing!, maxLines: 1, overflow: TextOverflow.ellipsis,
          style: const TextStyle(color: marketingGreen, fontSize: 11, fontWeight: FontWeight.w800))),
      ],
    ]),
  );
}

class MarketingEmptyState extends StatelessWidget {
  const MarketingEmptyState({super.key, required this.text, this.icon = Icons.inbox_outlined});
  final String text;
  final IconData icon;

  @override
  Widget build(BuildContext context) => Center(child: Padding(
    padding: const EdgeInsets.all(24),
    child: Column(mainAxisSize: MainAxisSize.min, children: [
      Icon(icon, color: const Color(0xFF9AA7A0), size: 38),
      const SizedBox(height: 10),
      Text(text, textAlign: TextAlign.center, style: const TextStyle(color: marketingMuted)),
    ]),
  ));
}

