import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'overtime_model.dart';

final overtimeStatusFilterProvider = StateProvider<String>((ref) => '');
final overtimePageProvider = StateProvider<int>((ref) => 1);
final overtimeDateStartProvider = StateProvider<DateTime?>((ref) => null);
final overtimeDateEndProvider = StateProvider<DateTime?>((ref) => null);
final overtimeKeywordProvider = StateProvider<String>((ref) => '');
final overtimeRefreshProvider = StateProvider<int>((ref) => 0);
final overtimeSortDirProvider = StateProvider<String>((ref) => 'DESC');

final now = DateTime.now();
final mockOvertimes = <OvertimeModel>[
  OvertimeModel(
    applyId: 1,
    billNo: 'JB20260520001',
    salesman: '张三',
    dept: '市场部',
    billType: '工作日加班',
    isAuditApproved: false,
    remark: '项目上线前紧急测试',
    createTime: DateTime(2026, 5, 20),
    updateTime: DateTime(2026, 5, 20),
    details: [
      OvertimeDetailModel(
        detailId: 1,
        applyId: 1,
        seqNo: 1,
        empCode: 'EMP001',
        shiftType: '白班',
        startDate: DateTime(2026, 5, 20),
        endDate: DateTime(2026, 5, 20),
        approvedHours: 2.5,
        isApproved: 'N',
        directOt: 'N',
        otQuantity: 2.5,
        reason: '测试加班',
      ),
    ],
  ),
  OvertimeModel(
    applyId: 2,
    billNo: 'JB20260518002',
    salesman: '王五',
    dept: '技术部',
    billType: '休息日加班',
    isAuditApproved: true,
    remark: '系统架构升级',
    createTime: DateTime(2026, 5, 17),
    updateTime: DateTime(2026, 5, 19),
    details: [
      OvertimeDetailModel(
        detailId: 2,
        applyId: 2,
        seqNo: 1,
        empCode: 'EMP002',
        shiftType: '白班',
        startDate: DateTime(2026, 5, 18),
        endDate: DateTime(2026, 5, 18),
        approvedHours: 7.5,
        isApproved: 'Y',
        directOt: 'N',
        otQuantity: 7.5,
        reason: '架构升级',
      ),
    ],
  ),
  OvertimeModel(
    applyId: 3,
    billNo: 'JB20260525003',
    salesman: '赵六',
    dept: '财务部',
    billType: '工作日加班',
    isAuditApproved: false,
    remark: '季度财务报表汇总',
    createTime: DateTime(2026, 5, 25),
    updateTime: DateTime(2026, 5, 25),
  ),
  OvertimeModel(
    applyId: 4,
    billNo: 'JB20260601004',
    salesman: '张三',
    dept: '市场部',
    billType: '节假日加班',
    isAuditApproved: false,
    remark: '节假日活动筹备',
    createTime: DateTime(2026, 5, 30),
    updateTime: DateTime(2026, 6, 1),
  ),
  OvertimeModel(
    applyId: 5,
    billNo: 'JB20260602005',
    salesman: '王五',
    dept: '技术部',
    billType: '工作日加班',
    isAuditApproved: false,
    remark: '夜间服务器维护',
    createTime: DateTime(2026, 6, 2),
    updateTime: DateTime(2026, 6, 2),
  ),
  OvertimeModel(
    applyId: 6,
    billNo: 'JB20260515006',
    salesman: '张三',
    dept: '市场部',
    billType: '休息日加班',
    isAuditApproved: true,
    remark: '周末展会布展',
    createTime: DateTime(2026, 5, 14),
    updateTime: DateTime(2026, 5, 15),
  ),
];

final overtimeListProvider = FutureProvider.family<List<OvertimeModel>, String>(
  (ref, status) async {
    ref.watch(overtimePageProvider);
    ref.watch(overtimeRefreshProvider);
    ref.watch(overtimeDateStartProvider);
    ref.watch(overtimeDateEndProvider);
    ref.watch(overtimeKeywordProvider);
    await Future.delayed(const Duration(milliseconds: 1000));

    var list = mockOvertimes.toList();

    // 按 isAuditApproved 过滤（'approved'/'pending'）
    if (status == 'approved') {
      list = list.where((e) => e.isAuditApproved).toList();
    } else if (status == 'pending') {
      list = list.where((e) => !e.isAuditApproved).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 keyword = ref.read(overtimeKeywordProvider);
    if (keyword.isNotEmpty) {
      final kw = keyword.toLowerCase();
      list = list
          .where(
            (e) =>
                e.billNo.toLowerCase().contains(kw) ||
                e.salesman.toLowerCase().contains(kw) ||
                e.remark.toLowerCase().contains(kw),
          )
          .toList();
    }

    final sortDir = ref.read(overtimeSortDirProvider);
    if (sortDir == 'ASC') {
      list.sort((a, b) => a.createTime.compareTo(b.createTime));
    } else {
      list.sort((a, b) => b.createTime.compareTo(a.createTime));
    }

    return list;
  },
);
