import 'package:flutter/foundation.dart';

import 'package:lanebow/models/user_model.dart';
import 'package:lanebow/models/club_model.dart';
import 'package:lanebow/models/member_model.dart';
import 'package:lanebow/services/auth_service.dart';
import 'package:lanebow/services/club_service.dart';
import 'package:lanebow/services/member_service.dart';

/// noSuchMethod 기반 간소 모킹 유틸
/// 실제 서비스 인터페이스가 방대하여 필요한 멤버만 구현하고,
/// 나머지는 noSuchMethod로 흡수합니다.

class MockAuthService extends ChangeNotifier implements AuthService {
  MockAuthService({required this.user, this.tokenValue = 'test_token'});

  final User user;
  final String tokenValue;

  @override
  bool get isAuthenticated => true;

  @override
  bool get isInitialized => true;

  @override
  bool get isLoading => false;

  @override
  String? get lastError => null;

  @override
  String? get token => tokenValue;

  @override
  User? get currentUser => user;

  @override
  dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}

class MockClubService extends ChangeNotifier implements ClubService {
  MockClubService({required this.club});

  Club? club;

  @override
  Club? get currentClub => club;

  @override
  bool get isLoading => false;

  @override
  List<Club> get clubs => club == null ? <Club>[] : <Club>[club!];

  @override
  Future<Club> updateClub(String clubId, Map<String, dynamic> updates) async {
    // 최소 동작: ownerId 변경 반영
    if (club != null && updates.containsKey('ownerId')) {
      club = Club(
        id: club!.id,
        name: club!.name,
        description: club!.description,
        logo: club!.logo,
        address: club!.address,
        location: club!.location,
        phone: club!.phone,
        email: club!.email,
        website: club!.website,
        memberCount: club!.memberCount,
        ownerId: updates['ownerId'] as String?,
        femaleHandicap: club!.femaleHandicap,
        averageCalculationPeriod: club!.averageCalculationPeriod,
        createdAt: club!.createdAt,
        updatedAt: club!.updatedAt,
        tieBreakerOptions: club!.tieBreakerOptions,
      );
      notifyListeners();
    }
    return club!;
  }

  @override
  Future<Club> fetchClub(String clubId) async {
    if (club == null) {
      throw Exception('클럽이 없습니다');
    }
    return club!;
  }

  @override
  dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}

class MockMemberService extends ChangeNotifier implements MemberService {
  MockMemberService({required List<Member> seed}) : _members = List<Member>.from(seed);

  List<Member> _members;

  @override
  bool get isLoading => false;

  @override
  String? get lastError => null;

  @override
  List<Member> get members => _members;

  @override
  Future<void> fetchClubMembers() async {
    // no-op
  }

  @override
  Future<void> setClubId(String clubId) async {
    // no-op
  }

  @override
  Future<List<Member>> fetchMembersByIds(String clubId, List<String> memberIds) async {
    if (memberIds.isEmpty) return <Member>[];
    return _members.where((m) => memberIds.contains(m.id)).toList();
  }

  set membersSeed(List<Member> next) {
    _members = List<Member>.from(next);
    notifyListeners();
  }

  @override
  dynamic noSuchMethod(Invocation invocation) => super.noSuchMethod(invocation);
}
