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

import '../../application/product_publishing_controller.dart';
import '../../domain/publish_status.dart';

/// Shows a [SnackBar] for the given [StatusActionResult].
///
/// Uses [KcColors.success] / [KcColors.danger] to match the design system.
/// This is a plain function — no broad notification infrastructure required.
void showStatusActionSnackBar(BuildContext context, StatusActionResult result) {
  final messenger = ScaffoldMessenger.maybeOf(context);
  if (messenger == null) return;

  final String message;
  final Color backgroundColor;
  final IconData icon;

  if (result.success) {
    final verb = _pastVerbForStatus(result.targetStatus);
    message = '${result.productName} $verb successfully.';
    backgroundColor = KcColors.success;
    icon = Icons.check_circle_outline;
  } else {
    final verb = _infinitiveVerbForStatus(result.targetStatus);
    message = 'Failed to $verb ${result.productName}.';
    backgroundColor = KcColors.danger;
    icon = Icons.error_outline;
  }

  messenger.hideCurrentSnackBar();
  messenger.showSnackBar(
    SnackBar(
      content: Row(
        children: [
          Icon(icon, color: Colors.white, size: 20),
          const SizedBox(width: 8),
          Expanded(child: Text(message)),
        ],
      ),
      backgroundColor: backgroundColor,
      behavior: SnackBarBehavior.floating,
      duration: result.success ? const Duration(seconds: 3) : const Duration(seconds: 5),
    ),
  );
}

/// Past-tense verb for success messages (e.g. "X published successfully").
String _pastVerbForStatus(PublishStatus status) {
  switch (status) {
    case PublishStatus.published:
      return 'published';
    case PublishStatus.draft:
      return 'moved to draft';
    case PublishStatus.pendingReview:
      return 'submitted for review';
    case PublishStatus.unpublished:
      return 'unpublished';
  }
}

/// Infinitive verb for failure messages (e.g. "Failed to publish X").
String _infinitiveVerbForStatus(PublishStatus status) {
  switch (status) {
    case PublishStatus.published:
      return 'publish';
    case PublishStatus.draft:
      return 'move to draft';
    case PublishStatus.pendingReview:
      return 'submit for review';
    case PublishStatus.unpublished:
      return 'unpublish';
  }
}

/// Shows a [SnackBar] for the given [PriceActionResult].
void showPriceActionSnackBar(BuildContext context, PriceActionResult result) {
  final messenger = ScaffoldMessenger.maybeOf(context);
  if (messenger == null) return;

  final String message;
  final Color backgroundColor;
  final IconData icon;

  if (result.success) {
    message = '${result.productName} price updated to \$${result.newPrice.toStringAsFixed(2)}.';
    backgroundColor = KcColors.success;
    icon = Icons.check_circle_outline;
  } else {
    message = 'Failed to update price for ${result.productName}.';
    backgroundColor = KcColors.danger;
    icon = Icons.error_outline;
  }

  messenger.hideCurrentSnackBar();
  messenger.showSnackBar(
    SnackBar(
      content: Row(
        children: [
          Icon(icon, color: Colors.white, size: 20),
          const SizedBox(width: 8),
          Expanded(child: Text(message)),
        ],
      ),
      backgroundColor: backgroundColor,
      behavior: SnackBarBehavior.floating,
      duration: result.success ? const Duration(seconds: 3) : const Duration(seconds: 5),
    ),
  );
}

/// Shows a [SnackBar] for the given [NameActionResult].
void showNameActionSnackBar(BuildContext context, NameActionResult result) {
  final messenger = ScaffoldMessenger.maybeOf(context);
  if (messenger == null) return;

  final String message;
  final Color backgroundColor;
  final IconData icon;

  if (result.success) {
    message = 'Product renamed to ${result.newName}.';
    backgroundColor = KcColors.success;
    icon = Icons.check_circle_outline;
  } else {
    message = 'Failed to rename ${result.productName}.';
    backgroundColor = KcColors.danger;
    icon = Icons.error_outline;
  }

  messenger.hideCurrentSnackBar();
  messenger.showSnackBar(
    SnackBar(
      content: Row(
        children: [
          Icon(icon, color: Colors.white, size: 20),
          const SizedBox(width: 8),
          Expanded(child: Text(message)),
        ],
      ),
      backgroundColor: backgroundColor,
      behavior: SnackBarBehavior.floating,
      duration: result.success ? const Duration(seconds: 3) : const Duration(seconds: 5),
    ),
  );
}
