import '../domain/product_draft.dart';
import '../domain/product_publishing_repository.dart';
import '../domain/publish_status.dart';

/// Stubbed implementation of [ProductPublishingRepository] with sample
/// Kell Creations products. No real WordPress/WooCommerce API calls are made.
class FakeProductPublishingRepository implements ProductPublishingRepository {
  final List<ProductDraft> _drafts = [
    ProductDraft(
      id: '1',
      name: 'Floral Bowl Cozy',
      description:
          'A beautifully crafted fabric bowl cozy with a vibrant floral pattern. '
          'Microwave-safe and perfect for keeping dishes warm at the table.',
      price: 12.99,
      sku: 'BC-FLR-001',
      category: 'Bowl Cozies',
      imageUrl: '',
      status: PublishStatus.published,
      lastModified: DateTime(2026, 3, 28),
    ),
    ProductDraft(
      id: '2',
      name: 'Citrus Coaster Set',
      description:
          'Set of four sublimated coasters featuring bright citrus designs. '
          'Heat-resistant cork backing protects surfaces.',
      price: 16.50,
      sku: 'CS-CIT-002',
      category: 'Coasters',
      imageUrl: '',
      status: PublishStatus.published,
      lastModified: DateTime(2026, 3, 25),
    ),
    ProductDraft(
      id: '3',
      name: 'Ocean Nightlight',
      description:
          'Sublimated ceramic nightlight with a calming ocean wave design. '
          'Includes LED bulb and plugs into any standard outlet.',
      price: 19.99,
      sku: 'NL-OCN-003',
      category: 'Nightlights',
      imageUrl: '',
      status: PublishStatus.pendingReview,
      lastModified: DateTime(2026, 4, 1),
    ),
    ProductDraft(
      id: '4',
      name: 'Fabric Jar Gripper',
      description:
          'Non-slip fabric jar gripper with a fun patterned design. '
          'Opens even the tightest lids with ease.',
      price: 8.50,
      sku: 'JG-BLU-004',
      category: 'Kitchen Accessories',
      imageUrl: '',
      status: PublishStatus.draft,
      lastModified: DateTime(2026, 4, 2),
    ),
    ProductDraft(
      id: '5',
      name: 'Skillet Handle Sleeve',
      description:
          'Quilted fabric sleeve that slips over hot skillet handles. '
          'Available in multiple patterns to match your kitchen décor.',
      price: 10.99,
      sku: 'SH-SUN-005',
      category: 'Kitchen Accessories',
      imageUrl: '',
      status: PublishStatus.draft,
      lastModified: DateTime(2026, 4, 3),
    ),
    ProductDraft(
      id: '6',
      name: 'Sublimated Slate Coaster',
      description:
          'Natural slate coaster with a full-color sublimated image. '
          'Felt feet protect furniture. Makes a great personalized gift.',
      price: 14.99,
      sku: 'SC-SUB-006',
      category: 'Coasters',
      imageUrl: '',
      status: PublishStatus.unpublished,
      lastModified: DateTime(2026, 3, 20),
    ),
  ];

  @override
  Future<List<ProductDraft>> getProductDrafts() async {
    // Simulate network latency.
    await Future<void>.delayed(const Duration(milliseconds: 300));
    return List.unmodifiable(_drafts);
  }

  @override
  Future<ProductDraft> publishDraft(String id) async {
    await Future<void>.delayed(const Duration(milliseconds: 500));

    final index = _drafts.indexWhere((d) => d.id == id);
    if (index == -1) {
      throw StateError('Draft with id $id not found');
    }

    final original = _drafts[index];
    final updated = original.copyWith(
      status: PublishStatus.published,
      lastModified: DateTime.now(),
    );
    _drafts[index] = updated;
    return updated;
  }

  @override
  Future<ProductDraft> updateProductStatus(String id, PublishStatus status) async {
    await Future<void>.delayed(const Duration(milliseconds: 400));

    final index = _drafts.indexWhere((d) => d.id == id);
    if (index == -1) {
      throw StateError('Draft with id $id not found');
    }

    final original = _drafts[index];
    final updated = original.copyWith(status: status, lastModified: DateTime.now());
    _drafts[index] = updated;
    return updated;
  }

  @override
  Future<ProductDraft> updateProductPrice(String id, double price) async {
    await Future<void>.delayed(const Duration(milliseconds: 400));

    final index = _drafts.indexWhere((d) => d.id == id);
    if (index == -1) {
      throw StateError('Draft with id $id not found');
    }

    final original = _drafts[index];
    final updated = original.copyWith(price: price, lastModified: DateTime.now());
    _drafts[index] = updated;
    return updated;
  }

  @override
  Future<ProductDraft> updateProductName(String id, String name) async {
    await Future<void>.delayed(const Duration(milliseconds: 400));

    final index = _drafts.indexWhere((d) => d.id == id);
    if (index == -1) {
      throw StateError('Draft with id $id not found');
    }

    final original = _drafts[index];
    final updated = original.copyWith(name: name, lastModified: DateTime.now());
    _drafts[index] = updated;
    return updated;
  }

  @override
  Future<ProductDraft> updateProductDescription(String id, String description) async {
    await Future<void>.delayed(const Duration(milliseconds: 400));

    final index = _drafts.indexWhere((d) => d.id == id);
    if (index == -1) {
      throw StateError('Draft with id $id not found');
    }

    final original = _drafts[index];
    final updated = original.copyWith(description: description, lastModified: DateTime.now());
    _drafts[index] = updated;
    return updated;
  }

  @override
  Future<ProductDraft> updateProductCategory(String id, String category) async {
    await Future<void>.delayed(const Duration(milliseconds: 400));

    final index = _drafts.indexWhere((d) => d.id == id);
    if (index == -1) {
      throw StateError('Draft with id $id not found');
    }

    final original = _drafts[index];
    final updated = original.copyWith(category: category, lastModified: DateTime.now());
    _drafts[index] = updated;
    return updated;
  }
}
