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

import '../api/endpoints/likes.dart';
import '../shared/widgets/lucide_heart.dart';
import '../theme/theme_extension.dart';
import 'likes_provider.dart';

class LikeButton extends ConsumerWidget {
  const LikeButton({
    required this.kind,
    required this.id,
    this.size = 22,
    super.key,
  });
  final LikeKind kind;
  final String id;
  final double size;

  @override
  Widget build(BuildContext context, WidgetRef ref) {
    final fs = Theme.of(context).extension<FabledSwordTheme>()!;
    final state = ref.watch(likedIdsProvider);
    final liked = state.maybeWhen(
      data: (s) => s.has(kind, id),
      orElse: () => false,
    );
    return IconButton(
      icon: LucideHeart(
        filled: liked,
        color: liked ? fs.accent : fs.ash,
        size: size,
      ),
      onPressed: () => ref.read(likesControllerProvider).toggle(kind, id),
    );
  }
}
