import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'vehicle_model.dart';

final vehicleStatusFilterProvider = StateProvider<String>((ref) => '');

final vehiclePageProvider = StateProvider<int>((ref) => 1);

final mockVehicles = <VehicleModel>[
  VehicleModel(
    id: 'veh-001',
    applicationNo: 'YC-20260520-001',
    applicantId: 'u-001',
    applicantName: '张三',
    deptId: 'dept-001',
    deptName: '市场部',
    vehicleId: '京A88888',
    purpose: 'reception',
    reason: '拜访重要客户，需前往深圳',
    origin: '公司总部',
    destination: '深圳分公司',
    passengerCount: 3,
    startTime: DateTime(2026, 5, 22, 8, 0),
    endTime: DateTime(2026, 5, 22, 18, 0),
    status: 'pending',
    approvalInstanceId: 'apr-v-001',
    createTime: DateTime(2026, 5, 20),
    updateTime: DateTime(2026, 5, 20),
  ),
  VehicleModel(
    id: 'veh-002',
    applicationNo: 'YC-20260525-002',
    applicantId: 'u-002',
    applicantName: '王五',
    deptId: 'dept-002',
    deptName: '技术部',
    vehicleId: '京B66666',
    purpose: 'business',
    reason: '运送服务器设备至数据中心',
    origin: '公司仓库',
    destination: '数据中心',
    passengerCount: 2,
    startTime: DateTime(2026, 5, 25, 9, 0),
    endTime: DateTime(2026, 5, 25, 17, 0),
    status: 'approved',
    approvalInstanceId: 'apr-v-002',
    createTime: DateTime(2026, 5, 23),
    updateTime: DateTime(2026, 5, 24),
  ),
  VehicleModel(
    id: 'veh-003',
    applicationNo: 'YC-20260601-003',
    applicantId: 'u-001',
    applicantName: '张三',
    deptId: 'dept-001',
    deptName: '市场部',
    vehicleId: '京C12345',
    purpose: 'official',
    reason: '前往税务局办理业务',
    origin: '公司总部',
    destination: '税务局',
    passengerCount: 1,
    startTime: DateTime(2026, 6, 1, 9, 0),
    endTime: DateTime(2026, 6, 1, 12, 0),
    status: 'draft',
    approvalInstanceId: '',
    createTime: DateTime(2026, 5, 31),
    updateTime: DateTime(2026, 5, 31),
  ),
  VehicleModel(
    id: 'veh-004',
    applicationNo: 'YC-20260528-004',
    applicantId: 'u-003',
    applicantName: '赵六',
    deptId: 'dept-003',
    deptName: '财务部',
    vehicleId: '京D99999',
    purpose: 'reception',
    reason: '接待审计团队',
    origin: '公司总部',
    destination: '机场',
    passengerCount: 4,
    startTime: DateTime(2026, 5, 28, 14, 0),
    endTime: DateTime(2026, 5, 28, 17, 0),
    status: 'returned',
    approvalInstanceId: 'apr-v-004',
    actualReturnTime: DateTime(2026, 5, 28, 16, 45),
    startOdometer: 12500.0,
    endOdometer: 12680.0,
    actualCost: 85.0,
    costRemark: '路桥费35元，停车费50元',
    createTime: DateTime(2026, 5, 27),
    updateTime: DateTime(2026, 5, 28),
  ),
  VehicleModel(
    id: 'veh-005',
    applicationNo: 'YC-20260515-005',
    applicantId: 'u-002',
    applicantName: '王五',
    deptId: 'dept-002',
    deptName: '技术部',
    vehicleId: '京E55555',
    purpose: 'business',
    reason: '参加技术交流会',
    origin: '公司总部',
    destination: '国际会展中心',
    passengerCount: 1,
    startTime: DateTime(2026, 5, 15, 8, 0),
    endTime: DateTime(2026, 5, 15, 18, 0),
    status: 'withdrawn',
    approvalInstanceId: '',
    createTime: DateTime(2026, 5, 14),
    updateTime: DateTime(2026, 5, 15),
  ),
  VehicleModel(
    id: 'veh-006',
    applicationNo: 'YC-20260510-006',
    applicantId: 'u-001',
    applicantName: '张三',
    deptId: 'dept-001',
    deptName: '市场部',
    vehicleId: '京A88888',
    purpose: 'reception',
    reason: '拜访客户',
    origin: '公司总部',
    destination: '客户公司',
    passengerCount: 2,
    startTime: DateTime(2026, 5, 10, 10, 0),
    endTime: DateTime(2026, 5, 10, 16, 0),
    status: 'rejected',
    approvalInstanceId: '',
    createTime: DateTime(2026, 5, 9),
    updateTime: DateTime(2026, 5, 10),
  ),
];

final vehicleDateStartProvider = StateProvider<DateTime?>((ref) => null);
final vehicleDateEndProvider = StateProvider<DateTime?>((ref) => null);
final vehiclePurposeFilterProvider = StateProvider<String?>((ref) => null);

final vehicleRefreshProvider = StateProvider<int>((ref) => 0);

final vehicleListProvider = FutureProvider
    .family<List<VehicleModel>, String>((ref, status) async {
      ref.watch(vehiclePageProvider);
      ref.watch(vehicleDateStartProvider);
      ref.watch(vehicleDateEndProvider);
      ref.watch(vehiclePurposeFilterProvider);
      ref.watch(vehicleRefreshProvider);
      // 模拟网络延迟，使骨架屏可见
      await Future.delayed(const Duration(milliseconds: 1000));

      var list = status.isEmpty
          ? mockVehicles
          : mockVehicles.where((e) => e.status == status).toList();

      final dateStart = ref.read(vehicleDateStartProvider);
      final dateEnd = ref.read(vehicleDateEndProvider);
      if (dateStart != null) {
        list = list
            .where(
              (e) => e.createTime.isAfter(
                dateStart.subtract(const Duration(days: 1)),
              ),
            )
            .toList();
      }
      if (dateEnd != null) {
        list = list
            .where(
              (e) =>
                  e.createTime.isBefore(dateEnd.add(const Duration(days: 1))),
            )
            .toList();
      }

      final purposeFilter = ref.read(vehiclePurposeFilterProvider);
      if (purposeFilter != null && purposeFilter.isNotEmpty) {
        list = list.where((e) => e.purpose == purposeFilter).toList();
      }

      return list;
    });
