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

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

final mockOvertimes = <OvertimeModel>[
  OvertimeModel(
    id: 'ot-001',
    applicationNo: 'JB-20260520-001',
    applicantId: 'u-001',
    applicantName: '张三',
    deptId: 'dept-001',
    deptName: '市场部',
    otType: 'workday',
    compensationType: 'overtime_pay',
    startTime: DateTime(2026, 5, 20, 18, 0),
    endTime: DateTime(2026, 5, 20, 21, 0),
    netOtHours: 2.5,
    reason: '项目上线前紧急测试',
    status: 'pending',
    approvalInstanceId: 'apr-001',
    createTime: DateTime(2026, 5, 20),
    updateTime: DateTime(2026, 5, 20),
  ),
  OvertimeModel(
    id: 'ot-002',
    applicationNo: 'JB-20260518-002',
    applicantId: 'u-002',
    applicantName: '王五',
    deptId: 'dept-002',
    deptName: '技术部',
    otType: 'rest_day',
    compensationType: 'comp_leave',
    startTime: DateTime(2026, 5, 18, 9, 0),
    endTime: DateTime(2026, 5, 18, 18, 0),
    netOtHours: 7.5,
    reason: '系统架构升级',
    status: 'approved',
    approvalInstanceId: 'apr-002',
    createTime: DateTime(2026, 5, 17),
    updateTime: DateTime(2026, 5, 19),
  ),
  OvertimeModel(
    id: 'ot-003',
    applicationNo: 'JB-20260525-003',
    applicantId: 'u-003',
    applicantName: '赵六',
    deptId: 'dept-003',
    deptName: '财务部',
    otType: 'workday',
    compensationType: 'mixed',
    compLeaveRatio: 0.3,
    startTime: DateTime(2026, 5, 25, 19, 0),
    endTime: DateTime(2026, 5, 25, 22, 0),
    netOtHours: 2.5,
    reason: '季度财务报表汇总',
    status: 'pending',
    approvalInstanceId: 'apr-003',
    createTime: DateTime(2026, 5, 25),
    updateTime: DateTime(2026, 5, 25),
  ),
  OvertimeModel(
    id: 'ot-004',
    applicationNo: 'JB-20260601-004',
    applicantId: 'u-001',
    applicantName: '张三',
    deptId: 'dept-001',
    deptName: '市场部',
    otType: 'legal_holiday',
    compensationType: 'overtime_pay',
    startTime: DateTime(2026, 6, 1, 10, 0),
    endTime: DateTime(2026, 6, 1, 15, 0),
    netOtHours: 4.0,
    reason: '节假日活动筹备',
    status: 'rejected',
    approvalInstanceId: '',
    createTime: DateTime(2026, 5, 30),
    updateTime: DateTime(2026, 6, 1),
  ),
  OvertimeModel(
    id: 'ot-005',
    applicationNo: 'JB-20260602-005',
    applicantId: 'u-002',
    applicantName: '王五',
    deptId: 'dept-002',
    deptName: '技术部',
    otType: 'workday',
    compensationType: 'comp_leave',
    startTime: DateTime(2026, 6, 2, 18, 30),
    endTime: DateTime(2026, 6, 2, 21, 0),
    netOtHours: 2.0,
    reason: '夜间服务器维护',
    status: 'draft',
    approvalInstanceId: '',
    createTime: DateTime(2026, 6, 2),
    updateTime: DateTime(2026, 6, 2),
  ),
  OvertimeModel(
    id: 'ot-006',
    applicationNo: 'JB-20260515-006',
    applicantId: 'u-001',
    applicantName: '张三',
    deptId: 'dept-001',
    deptName: '市场部',
    otType: 'rest_day',
    compensationType: 'overtime_pay',
    startTime: DateTime(2026, 5, 15, 9, 0),
    endTime: DateTime(2026, 5, 15, 17, 0),
    netOtHours: 7.0,
    reason: '周末展会布展',
    status: 'withdrawn',
    approvalInstanceId: '',
    createTime: DateTime(2026, 5, 14),
    updateTime: DateTime(2026, 5, 15),
  ),
];

final overtimePageProvider = StateProvider<int>((ref) => 1);
final overtimeDateStartProvider = StateProvider<DateTime?>((ref) => null);
final overtimeDateEndProvider = StateProvider<DateTime?>((ref) => null);
final overtimeTypeFilterProvider = StateProvider<String?>((ref) => null);

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

final overtimeListProvider = FutureProvider
    .family<List<OvertimeModel>, String>((ref, status) async {
      ref.watch(overtimePageProvider);
      ref.watch(overtimeDateStartProvider);
      ref.watch(overtimeDateEndProvider);
      ref.watch(overtimeTypeFilterProvider);
      ref.watch(overtimeRefreshProvider);
      // 模拟网络延迟，使骨架屏可见
      await Future.delayed(const Duration(milliseconds: 1000));

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

      final dateStart = ref.read(overtimeDateStartProvider);
      final dateEnd = ref.read(overtimeDateEndProvider);
      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 typeFilter = ref.read(overtimeTypeFilterProvider);
      if (typeFilter != null && typeFilter.isNotEmpty) {
        list = list.where((e) => e.otType == typeFilter).toList();
      }

      return list;
    });
