import 'dart:typed_data';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../domain/parsed_receipt_item.dart';

// ── Destination-enum ──────────────────────────────────────────────────────────

enum ImportDestination { inventory, pantry }

enum CategorySelectionSource { ai, manual }

// ── Per-rad redigeringstillstånd ──────────────────────────────────────────────

class ItemEdit {
  final int? productId;
  final String? productName;
  final int? categoryId;
  final String? categoryPath;
  final CategorySelectionSource? categorySource;
  final double? quantity;
  final String? unit;
  final double? packQuantity;
  final String? packUnit;
  final double? packageCount;
  final ImportDestination destination;

  const ItemEdit({
    this.productId,
    this.productName,
    this.categoryId,
    this.categoryPath,
    this.categorySource,
    this.quantity,
    this.unit,
    this.packQuantity,
    this.packUnit,
    this.packageCount,
    this.destination = ImportDestination.inventory,
  });
}

// ── Session-state ─────────────────────────────────────────────────────────────

class ReceiptImportSession {
  final Uint8List? fileBytes;
  final String? fileExtension;
  final List<ParsedReceiptItem>? items; // null = ej parsad än
  final Map<int, ItemEdit> edits;
  final Map<int, bool> selected;

  const ReceiptImportSession({
    this.fileBytes,
    this.fileExtension,
    this.items,
    this.edits = const {},
    this.selected = const {},
  });

  ReceiptImportSession copyWith({
    Uint8List? fileBytes,
    String? fileExtension,
    List<ParsedReceiptItem>? items,
    Map<int, ItemEdit>? edits,
    Map<int, bool>? selected,
  }) =>
      ReceiptImportSession(
        fileBytes: fileBytes ?? this.fileBytes,
        fileExtension: fileExtension ?? this.fileExtension,
        items: items ?? this.items,
        edits: edits ?? this.edits,
        selected: selected ?? this.selected,
      );
}

// ── Notifier ──────────────────────────────────────────────────────────────────

class ReceiptImportSessionNotifier
    extends Notifier<ReceiptImportSession?> {
  @override
  ReceiptImportSession? build() => null;

  /// Ny fil vald — återställer items/edits/selected, behåller ingenting gammalt.
  void setFile(Uint8List bytes, String extension) {
    state = ReceiptImportSession(fileBytes: bytes, fileExtension: extension);
  }

  void setItems(List<ParsedReceiptItem> items) {
    // Bevara filinformationen när items sätts
    state = (state ?? const ReceiptImportSession()).copyWith(items: items);
  }

  void setEdit(int index, ItemEdit edit) {
    if (state == null) return;
    final edits = Map<int, ItemEdit>.from(state!.edits)..[index] = edit;
    state = state!.copyWith(edits: edits);
  }

  void setSelected(int index, bool value) {
    if (state == null) return;
    final selected = Map<int, bool>.from(state!.selected)..[index] = value;
    state = state!.copyWith(selected: selected);
  }

  void clear() => state = null;
}

final receiptImportSessionProvider =
    NotifierProvider<ReceiptImportSessionNotifier, ReceiptImportSession?>(
  ReceiptImportSessionNotifier.new,
);
