import '../domain/inventory_adjustment_result.dart';
import '../domain/inventory_item.dart';
import '../domain/inventory_repository.dart';
import '../domain/inventory_status.dart';

class FakeInventoryRepository implements InventoryRepository {
  /// Mutable in-memory store — seeded on construction.
  final List<InventoryItem> _items = _buildSeedData();

  static List<InventoryItem> _buildSeedData() {
    final now = DateTime(2026, 7, 1);
    return [
      InventoryItem(
        id: '1',
        sku: 'BC-FLR-001',
        name: 'Floral Bowl Cozy',
        description: 'Hand-sewn fabric bowl cozy with floral print. Microwave safe.',
        imageUrl: 'https://example.com/images/bc-flr-001.jpg',
        category: 'Kitchen Accessories',
        quantityOnHand: 18,
        unitPrice: 12.99,
        reorderPoint: 5,
        supplier: 'Kell Creations',
        location: 'Shelf A1',
        status: InventoryStatus.inStock,
        lastUpdated: now,
      ),
      InventoryItem(
        id: '2',
        sku: 'CS-CIT-002',
        name: 'Citrus Coaster Set',
        description: 'Set of 4 sublimated slate coasters with citrus fruit designs.',
        imageUrl: 'https://example.com/images/cs-cit-002.jpg',
        category: 'Home Decor',
        quantityOnHand: 7,
        unitPrice: 16.50,
        reorderPoint: 10,
        supplier: 'Kell Creations',
        location: 'Shelf B2',
        status: InventoryStatus.lowStock,
        lastUpdated: now,
      ),
      InventoryItem(
        id: '3',
        sku: 'NL-OCN-003',
        name: 'Ocean Nightlight',
        description: 'Sublimated acrylic nightlight with ocean wave design.',
        imageUrl: 'https://example.com/images/nl-ocn-003.jpg',
        category: 'Home Decor',
        quantityOnHand: 0,
        unitPrice: 19.99,
        reorderPoint: 3,
        supplier: null,
        location: 'Shelf C1',
        status: InventoryStatus.outOfStock,
        lastUpdated: now,
      ),
      InventoryItem(
        id: '4',
        sku: 'JG-BLU-004',
        name: 'Fabric Jar Gripper',
        description: 'Quilted fabric jar gripper for easy lid opening.',
        imageUrl: 'https://example.com/images/jg-blu-004.jpg',
        category: 'Kitchen Accessories',
        quantityOnHand: 23,
        unitPrice: 8.50,
        reorderPoint: 8,
        supplier: 'Kell Creations',
        location: 'Shelf A2',
        status: InventoryStatus.inStock,
        lastUpdated: now,
      ),
      InventoryItem(
        id: '5',
        sku: 'SH-SUN-005',
        name: 'Skillet Handle Sleeve',
        description: 'Heat-resistant fabric sleeve for cast iron skillet handles.',
        imageUrl: 'https://example.com/images/sh-sun-005.jpg',
        category: 'Kitchen Accessories',
        quantityOnHand: 5,
        unitPrice: 10.99,
        reorderPoint: 8,
        supplier: 'Kell Creations',
        location: null,
        status: InventoryStatus.lowStock,
        lastUpdated: now,
      ),
      InventoryItem(
        id: '6',
        sku: 'SC-SUB-006',
        name: 'Sublimated Slate Coaster',
        description: 'Single sublimated slate coaster — prototype/draft item.',
        imageUrl: '',
        category: 'Home Decor',
        quantityOnHand: 0,
        unitPrice: 14.99,
        reorderPoint: 5,
        supplier: null,
        location: null,
        status: InventoryStatus.draft,
        lastUpdated: now,
      ),
    ];
  }

  @override
  Future<List<InventoryItem>> getInventoryItems() async {
    await Future<void>.delayed(const Duration(milliseconds: 300));
    return List.unmodifiable(_items);
  }

  @override
  Future<InventoryAdjustmentResult> adjustQuantity(String id, int delta, String reason) async {
    await Future<void>.delayed(const Duration(milliseconds: 150));
    final index = _items.indexWhere((i) => i.id == id);
    if (index == -1) {
      return const InventoryAdjustmentResult.failure('Item not found');
    }
    final item = _items[index];
    final newQty = item.quantityOnHand + delta;
    if (newQty < 0) {
      return const InventoryAdjustmentResult.failure('Quantity cannot go below zero');
    }
    final updated = item.copyWith(
      quantityOnHand: newQty,
      status: _deriveStatus(newQty, item.reorderPoint),
      lastUpdated: DateTime.now(),
    );
    _items[index] = updated;
    return InventoryAdjustmentResult.success(updated);
  }

  @override
  Future<InventoryAdjustmentResult> setQuantity(String id, int quantity, String reason) async {
    await Future<void>.delayed(const Duration(milliseconds: 150));
    if (quantity < 0) {
      return const InventoryAdjustmentResult.failure('Quantity cannot be negative');
    }
    final index = _items.indexWhere((i) => i.id == id);
    if (index == -1) {
      return const InventoryAdjustmentResult.failure('Item not found');
    }
    final item = _items[index];
    final updated = item.copyWith(
      quantityOnHand: quantity,
      status: _deriveStatus(quantity, item.reorderPoint),
      lastUpdated: DateTime.now(),
    );
    _items[index] = updated;
    return InventoryAdjustmentResult.success(updated);
  }

  @override
  Future<InventoryItem> createItem(InventoryItem item) async {
    await Future<void>.delayed(const Duration(milliseconds: 150));
    _items.add(item);
    return item;
  }

  @override
  Future<InventoryItem> updateItem(InventoryItem item) async {
    await Future<void>.delayed(const Duration(milliseconds: 150));
    final index = _items.indexWhere((i) => i.id == item.id);
    if (index == -1) {
      throw StateError('Item with id ${item.id} not found');
    }
    final updated = item.copyWith(lastUpdated: DateTime.now());
    _items[index] = updated;
    return updated;
  }

  @override
  Future<InventoryAdjustmentResult> updateItemImage(String id, String imageUrl) async {
    await Future<void>.delayed(const Duration(milliseconds: 150));
    final index = _items.indexWhere((i) => i.id == id);
    if (index == -1) {
      return const InventoryAdjustmentResult.failure('Item not found');
    }
    final updated = _items[index].copyWith(imageUrl: imageUrl, lastUpdated: DateTime.now());
    _items[index] = updated;
    return InventoryAdjustmentResult.success(updated);
  }

  // ── Private helpers ────────────────────────────────────────────────────────

  static InventoryStatus _deriveStatus(int quantity, int reorderPoint) {
    if (quantity == 0) return InventoryStatus.outOfStock;
    if (quantity <= reorderPoint) return InventoryStatus.lowStock;
    return InventoryStatus.inStock;
  }
}
