import 'package:flutter_riverpod/flutter_riverpod.dart';

import '../../core/api/api_client.dart';
import '../public/public_api.dart';

class CartLine {
  CartLine({
    required this.menuItemId,
    required this.name,
    required this.unitPrice,
    this.quantity = 1,
    this.notes,
  });

  final String menuItemId;
  final String name;
  final int unitPrice;
  int quantity;
  final String? notes;

  int get lineTotal => unitPrice * quantity;

  Map<String, dynamic> toOrderJson() => {
        'menuItemId': menuItemId,
        'quantity': quantity,
        if (notes != null && notes!.isNotEmpty) 'notes': notes,
      };
}

class CartState {
  CartState({
    this.cafeSlug,
    this.cafeName,
    this.tableId,
    this.tableNumber,
    this.lines = const [],
  });

  final String? cafeSlug;
  final String? cafeName;
  final String? tableId;
  final int? tableNumber;
  final List<CartLine> lines;

  int get itemCount => lines.fold(0, (sum, l) => sum + l.quantity);
  int get subtotal => lines.fold(0, (sum, l) => sum + l.lineTotal);

  CartState copyWith({
    String? cafeSlug,
    String? cafeName,
    String? tableId,
    int? tableNumber,
    List<CartLine>? lines,
  }) =>
      CartState(
        cafeSlug: cafeSlug ?? this.cafeSlug,
        cafeName: cafeName ?? this.cafeName,
        tableId: tableId ?? this.tableId,
        tableNumber: tableNumber ?? this.tableNumber,
        lines: lines ?? this.lines,
      );
}

class CartNotifier extends StateNotifier<CartState> {
  CartNotifier() : super(CartState());

  void setContext({
    required String slug,
    required String cafeName,
    String? tableId,
    int? tableNumber,
  }) {
    state = CartState(
      cafeSlug: slug,
      cafeName: cafeName,
      tableId: tableId,
      tableNumber: tableNumber,
      lines: state.lines,
    );
  }

  void addItem(CartLine line) {
    final existing = state.lines.where((l) => l.menuItemId == line.menuItemId).toList();
    if (existing.isNotEmpty) {
      existing.first.quantity += line.quantity;
      state = state.copyWith(lines: [...state.lines]);
      return;
    }
    state = state.copyWith(lines: [...state.lines, line]);
  }

  void removeItem(String menuItemId) {
    state = state.copyWith(
      lines: state.lines.where((l) => l.menuItemId != menuItemId).toList(),
    );
  }

  void clear() => state = CartState();
}

final apiClientProvider = Provider<ApiClient>((ref) => ApiClient());

final publicApiProvider = Provider<PublicApi>((ref) => PublicApi(ref.watch(apiClientProvider)));

final cartProvider = StateNotifierProvider<CartNotifier, CartState>((ref) => CartNotifier());
