import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../../core/models/analytics.dart';
import '../../../core/models/vehicle.dart';
import '../../../core/models/reservation.dart';
import '../../../core/models/customer.dart';
import '../../../core/models/notification.dart';
import '../../../core/models/offer.dart';
import '../../../core/models/settings.dart';
import '../../../core/models/report.dart';
import '../../../core/services/analytics_service.dart';
import '../../../core/services/vehicle_service.dart';
import '../../../core/services/reservation_service.dart';
import '../../../core/services/customer_service.dart';
import '../../../core/services/notification_service.dart';
import '../../../core/services/offer_service.dart';
import '../../../core/services/settings_service.dart';
import '../../../core/services/payment_service.dart';
import '../../../core/services/team_service.dart';
import '../../../core/services/contract_service.dart';

// ─── Service providers ────────────────────────────────────────

final analyticsServiceProvider = Provider((_) => AnalyticsService());
final vehicleServiceProvider = Provider((_) => VehicleService());
final reservationServiceProvider = Provider((_) => ReservationService());
final customerServiceProvider = Provider((_) => CustomerService());
final notificationServiceProvider = Provider((_) => NotificationService());
final offerServiceProvider = Provider((_) => OfferService());
final settingsServiceProvider = Provider((_) => SettingsService());

// ─── Analytics ────────────────────────────────────────────────

final dashboardMetricsProvider = FutureProvider<DashboardMetrics>((ref) async {
  return ref.read(analyticsServiceProvider).getDashboard();
});

final reportProvider =
    FutureProvider.family<ReportSummary, Map<String, String>>((ref, params) async {
  return ref.read(analyticsServiceProvider).getReport(
        startDate: params['startDate']!,
        endDate: params['endDate']!,
        status: params['status'],
      );
});

// ─── Vehicles ─────────────────────────────────────────────────

final vehiclesProvider = FutureProvider.family<VehicleListResponse, Map<String, dynamic>>(
  (ref, params) async {
    return ref.read(vehicleServiceProvider).getVehicles(
          page: params['page'] as int? ?? 1,
          pageSize: params['pageSize'] as int? ?? 20,
          status: params['status'] as String?,
          search: params['search'] as String?,
        );
  },
);

final vehicleDetailProvider = FutureProvider.family<Vehicle, String>((ref, id) {
  return ref.read(vehicleServiceProvider).getVehicle(id);
});

final maintenanceProvider =
    FutureProvider.family<List<MaintenanceLog>, String>((ref, vehicleId) {
  return ref.read(vehicleServiceProvider).getMaintenance(vehicleId);
});

typedef CalendarKey = ({String vehicleId, int year, int month});

final calendarProvider =
    FutureProvider.family<List<CalendarEvent>, CalendarKey>((ref, key) {
  return ref.read(vehicleServiceProvider)
      .getCalendar(key.vehicleId, key.year, key.month);
});

// ─── Reservations ─────────────────────────────────────────────

final reservationsProvider =
    FutureProvider.family<ReservationListResponse, Map<String, dynamic>>(
  (ref, params) async {
    return ref.read(reservationServiceProvider).getReservations(
          page: params['page'] as int? ?? 1,
          pageSize: params['pageSize'] as int? ?? 20,
          status: params['status'] as String?,
          source: params['source'] as String?,
          search: params['search'] as String?,
          customerId: params['customerId'] as String?,
        );
  },
);

final reservationDetailProvider =
    FutureProvider.family<Reservation, String>((ref, id) {
  return ref.read(reservationServiceProvider).getReservation(id);
});

final inspectionsProvider =
    FutureProvider.family<List<ReservationInspection>, String>((ref, id) {
  return ref.read(reservationServiceProvider).getInspections(id);
});

// ─── Customers ────────────────────────────────────────────────

final customersProvider =
    FutureProvider.family<CustomerListResponse, Map<String, dynamic>>(
  (ref, params) async {
    return ref.read(customerServiceProvider).getCustomers(
          page: params['page'] as int? ?? 1,
          pageSize: params['pageSize'] as int? ?? 20,
          search: params['search'] as String?,
        );
  },
);

final customerDetailProvider =
    FutureProvider.family<Customer, String>((ref, id) {
  return ref.read(customerServiceProvider).getCustomer(id);
});

// ─── Notifications ────────────────────────────────────────────

final notificationsProvider =
    FutureProvider<List<AppNotification>>((ref) async {
  return ref.read(notificationServiceProvider).getNotifications();
});

final unreadCountProvider = FutureProvider<int>((ref) async {
  return ref.read(notificationServiceProvider).getUnreadCount();
});

// ─── Offers ───────────────────────────────────────────────────

final offersProvider = FutureProvider<List<CompanyOffer>>((ref) async {
  return ref.read(offerServiceProvider).getOffers();
});

final offerDetailProvider =
    FutureProvider.family<CompanyOffer, String>((ref, id) {
  return ref.read(offerServiceProvider).getOffer(id);
});

// ─── Settings ─────────────────────────────────────────────────

final brandSettingsProvider = FutureProvider<BrandSettings>((ref) async {
  return ref.read(settingsServiceProvider).getBrand();
});

// ─── Payments ─────────────────────────────────────────────────

final paymentServiceProvider = Provider((_) => PaymentService());

final reservationPaymentsProvider =
    FutureProvider.family<List<Payment>, String>((ref, reservationId) {
  return ref.read(paymentServiceProvider).getPayments(reservationId);
});

// ─── Team ─────────────────────────────────────────────────────

final teamServiceProvider = Provider((_) => TeamService());

final teamProvider = FutureProvider<List<TeamMember>>((ref) {
  return ref.read(teamServiceProvider).getMembers();
});

// ─── Contracts ────────────────────────────────────────────────

final contractServiceProvider = Provider((_) => ContractService());

final contractProvider =
    FutureProvider.family<ContractPayload, String>((ref, reservationId) {
  return ref.read(contractServiceProvider).getContract(reservationId);
});
