import 'package:flutter_riverpod/flutter_riverpod.dart';
import '../../shared/models/approval_status.dart';
import 'expense_model.dart';

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

final expenseScopeProvider = StateProvider<String>((ref) => 'mine');

final mockExpenses = <ExpenseModel>[
  ExpenseModel(
    id: 'exp-001',
    reportNo: 'BX202605001',
    applicantId: 'u-001',
    applicantName: '张三',
    deptId: 'dept-001',
    deptName: '市场部',
    expenseType: '差旅费',
    totalAmount: 2580.00,
    invoiceCount: 3,
    status: 'pending',
    purpose: '北京出差拜访客户',
    accountBankName: '中国银行',
    accountHolderName: '张三',
    accountName: '张三',
    accountId: '6217001234567890123',
    paymentStatus: 'unpaid',
    projectName: '华北区客户拓展',
    budgetSubjectName: '差旅费预算',
    sourceApplicationId: 'EA001',
    sourceImportAmount: 2000.00,
    attachments: ['http://example.com/invoice1.jpg'],
    createTime: DateTime(2026, 5, 20),
    updateTime: DateTime(2026, 5, 20),
    details: [
      ExpenseDetailModel(
        id: 'det-001',
        expenseId: 'exp-001',
        expenseDate: DateTime(2026, 5, 19),
        expenseType: '交通费',
        expenseDesc: '北京-上海高铁',
        amount: 553.00,
        taxAmount: 27.65,
        totalAmount: 580.65,
        baseAmount: 580.65,
        currency: 'CNY',
        exchangeRate: 1.0,
        invoiceType: 'general',
        invoiceNo: 'INV20260519001',
        invoiceCode: '3100234567',
        taxRate: 0.05,
      ),
      ExpenseDetailModel(
        id: 'det-002',
        expenseId: 'exp-001',
        expenseDate: DateTime(2026, 5, 19),
        expenseType: '住宿费',
        expenseDesc: '上海酒店住宿',
        amount: 800.00,
        taxAmount: 40.00,
        totalAmount: 840.00,
        baseAmount: 840.00,
        currency: 'CNY',
        exchangeRate: 1.0,
        invoiceType: 'special',
        invoiceNo: 'INV20260519002',
        invoiceCode: '3100456789',
        taxRate: 0.05,
      ),
      ExpenseDetailModel(
        id: 'det-003',
        expenseId: 'exp-001',
        expenseDate: DateTime(2026, 5, 20),
        expenseType: '餐饮费',
        expenseDesc: '客户午餐',
        amount: 1100.00,
        taxAmount: 55.00,
        totalAmount: 1155.00,
        baseAmount: 1155.00,
        currency: 'CNY',
        exchangeRate: 1.0,
        invoiceType: 'general',
        invoiceNo: 'INV20260520003',
        invoiceCode: '3100567890',
        taxRate: 0.05,
      ),
    ],
    approvalRecords: [
      ApprovalRecord(
        id: 'ar-001',
        bizId: 'exp-001',
        bizType: 'expense',
        approverId: 'u-mgr',
        approverName: '李经理',
        approvalLevel: 1,
        action: 'pending',
        opinion: '',
        approvalTime: DateTime(2026, 5, 21),
      ),
    ],
  ),
  ExpenseModel(
    id: 'exp-002',
    reportNo: 'BX202605002',
    applicantId: 'u-001',
    applicantName: '张三',
    deptId: 'dept-001',
    deptName: '市场部',
    expenseType: '办公用品',
    totalAmount: 1280.50,
    invoiceCount: 2,
    status: 'approved',
    purpose: '部门办公用品采购',
    accountBankName: '中国银行',
    accountHolderName: '张三',
    accountName: '张三',
    accountId: '6217001234567890123',
    paymentStatus: 'paid',
    voucherNo: 'V202606001',
    projectName: '办公运营',
    budgetSubjectName: '办公费预算',
    createTime: DateTime(2026, 5, 15),
    updateTime: DateTime(2026, 5, 18),
    details: [
      ExpenseDetailModel(
        id: 'det-004',
        expenseId: 'exp-002',
        expenseDate: DateTime(2026, 5, 14),
        expenseType: '办公用品',
        expenseDesc: '打印纸、墨盒',
        amount: 1280.50,
        taxAmount: 64.03,
        totalAmount: 1344.53,
        baseAmount: 1344.53,
        currency: 'CNY',
        exchangeRate: 1.0,
        invoiceType: 'special',
        invoiceNo: 'INV20260514001',
        invoiceCode: '4400123456',
        taxRate: 0.05,
      ),
    ],
    approvalRecords: [
      ApprovalRecord(
        id: 'ar-002',
        bizId: 'exp-002',
        bizType: 'expense',
        approverId: 'u-mgr',
        approverName: '李经理',
        approvalLevel: 1,
        action: 'approve',
        opinion: '同意',
        approvalTime: DateTime(2026, 5, 18),
      ),
    ],
  ),
  ExpenseModel(
    id: 'exp-003',
    reportNo: 'BX202605003',
    applicantId: 'u-003',
    applicantName: '赵六',
    deptId: 'dept-003',
    deptName: '财务部',
    expenseType: '业务招待费',
    totalAmount: 3600.00,
    invoiceCount: 1,
    status: 'rejected',
    purpose: '客户答谢晚宴',
    accountBankName: '工商银行',
    accountHolderName: '赵六',
    accountName: '赵六',
    accountId: '6222009876543210987',
    paymentStatus: 'unpaid',
    createTime: DateTime(2026, 5, 10),
    updateTime: DateTime(2026, 5, 12),
    details: [
      ExpenseDetailModel(
        id: 'det-005',
        expenseId: 'exp-003',
        expenseDate: DateTime(2026, 5, 9),
        expenseType: '招待费',
        expenseDesc: '客户晚宴',
        amount: 3600.00,
        taxAmount: 0.0,
        totalAmount: 3600.00,
        baseAmount: 3600.00,
        currency: 'CNY',
        exchangeRate: 1.0,
        invoiceType: 'none',
        invoiceNo: '',
        invoiceCode: '',
        taxRate: 0.0,
      ),
    ],
    approvalRecords: [
      ApprovalRecord(
        id: 'ar-003',
        bizId: 'exp-003',
        bizType: 'expense',
        approverId: 'u-mgr',
        approverName: '李经理',
        approvalLevel: 1,
        action: 'reject',
        opinion: '发票不符合规定',
        approvalTime: DateTime(2026, 5, 12),
      ),
    ],
  ),
  ExpenseModel(
    id: 'exp-004',
    reportNo: 'BX202605004',
    applicantId: 'u-004',
    applicantName: '钱七',
    deptId: 'dept-004',
    deptName: '技术部',
    expenseType: '交通费',
    totalAmount: 890.00,
    invoiceCount: 1,
    status: 'pending',
    purpose: '市内出行',
    accountBankName: '建设银行',
    accountHolderName: '钱七',
    accountName: '钱七',
    accountId: '6217001111222233334',
    paymentStatus: 'unpaid',
    createTime: DateTime(2026, 5, 22),
    updateTime: DateTime(2026, 5, 22),
    details: [
      ExpenseDetailModel(
        id: 'det-006',
        expenseId: 'exp-004',
        expenseDate: DateTime(2026, 5, 21),
        expenseType: '交通费',
        expenseDesc: '市内打车',
        amount: 890.00,
        taxAmount: 44.50,
        totalAmount: 934.50,
        baseAmount: 934.50,
        currency: 'CNY',
        exchangeRate: 1.0,
        invoiceType: 'general',
        invoiceNo: 'INV20260521001',
        invoiceCode: '5100123456',
        taxRate: 0.05,
      ),
    ],
  ),
  ExpenseModel(
    id: 'exp-005',
    reportNo: 'BX202605005',
    applicantId: 'u-001',
    applicantName: '张三',
    deptId: 'dept-001',
    deptName: '市场部',
    expenseType: '差旅费',
    totalAmount: 4200.00,
    invoiceCount: 4,
    status: 'approved',
    purpose: '深圳出差',
    accountBankName: '中国银行',
    accountHolderName: '张三',
    accountName: '张三',
    accountId: '6217001234567890123',
    paymentStatus: 'unpaid',
    voucherNo: '',
    projectName: '华南区客户拓展',
    budgetSubjectName: '差旅费预算',
    createTime: DateTime(2026, 5, 25),
    updateTime: DateTime(2026, 5, 28),
    details: [
      ExpenseDetailModel(
        id: 'det-007',
        expenseId: 'exp-005',
        expenseDate: DateTime(2026, 5, 24),
        expenseType: '交通费',
        expenseDesc: '深圳往返机票',
        amount: 2500.00,
        taxAmount: 125.00,
        totalAmount: 2625.00,
        baseAmount: 2625.00,
        currency: 'CNY',
        exchangeRate: 1.0,
        invoiceType: 'special',
        invoiceNo: 'INV20260524001',
        invoiceCode: '4400789012',
        taxRate: 0.05,
      ),
      ExpenseDetailModel(
        id: 'det-008',
        expenseId: 'exp-005',
        expenseDate: DateTime(2026, 5, 24),
        expenseType: '住宿费',
        expenseDesc: '深圳酒店住宿',
        amount: 1200.00,
        taxAmount: 60.00,
        totalAmount: 1260.00,
        baseAmount: 1260.00,
        currency: 'CNY',
        exchangeRate: 1.0,
        invoiceType: 'special',
        invoiceNo: 'INV20260524002',
        invoiceCode: '4400789013',
        taxRate: 0.05,
      ),
    ],
    approvalRecords: [
      ApprovalRecord(
        id: 'ar-004',
        bizId: 'exp-005',
        bizType: 'expense',
        approverId: 'u-mgr',
        approverName: '李经理',
        approvalLevel: 1,
        action: 'approve',
        opinion: '同意',
        approvalTime: DateTime(2026, 5, 28),
      ),
    ],
  ),
  ExpenseModel(
    id: 'exp-006',
    reportNo: 'BX202605006',
    applicantId: 'u-001',
    applicantName: '张三',
    deptId: 'dept-001',
    deptName: '市场部',
    expenseType: '交通费',
    totalAmount: 350.00,
    invoiceCount: 1,
    status: 'draft',
    purpose: '日常办公用车',
    accountBankName: '中国银行',
    accountHolderName: '张三',
    accountName: '张三',
    accountId: '6217001234567890123',
    paymentStatus: 'unpaid',
    createTime: DateTime(2026, 6, 1),
    updateTime: DateTime(2026, 6, 1),
    details: [
      ExpenseDetailModel(
        id: 'det-009',
        expenseId: 'exp-006',
        expenseDate: DateTime(2026, 6, 1),
        expenseType: '交通费',
        expenseDesc: '打车',
        amount: 350.00,
        taxAmount: 17.50,
        totalAmount: 367.50,
        baseAmount: 367.50,
        currency: 'CNY',
        exchangeRate: 1.0,
        invoiceType: 'general',
        invoiceNo: 'INV20260601001',
        invoiceCode: '5100789012',
        taxRate: 0.05,
      ),
    ],
  ),
  ExpenseModel(
    id: 'exp-007',
    reportNo: 'BX202606001',
    applicantId: 'u-001',
    applicantName: '张三',
    deptId: 'dept-001',
    deptName: '市场部',
    expenseType: '差旅费',
    totalAmount: 1800.00,
    invoiceCount: 2,
    status: 'revoked',
    purpose: '上海出差（已撤回）',
    accountBankName: '中国银行',
    accountHolderName: '张三',
    accountName: '张三',
    accountId: '6217001234567890123',
    paymentStatus: 'unpaid',
    createTime: DateTime(2026, 6, 2),
    updateTime: DateTime(2026, 6, 3),
    details: [
      ExpenseDetailModel(
        id: 'det-010',
        expenseId: 'exp-007',
        expenseDate: DateTime(2026, 6, 2),
        expenseType: '交通费',
        expenseDesc: '上海往返高铁',
        amount: 1100.00,
        taxAmount: 55.00,
        totalAmount: 1155.00,
        baseAmount: 1155.00,
        currency: 'CNY',
        exchangeRate: 1.0,
        invoiceType: 'general',
        invoiceNo: 'INV20260602001',
        invoiceCode: '3100890123',
        taxRate: 0.05,
      ),
      ExpenseDetailModel(
        id: 'det-011',
        expenseId: 'exp-007',
        expenseDate: DateTime(2026, 6, 2),
        expenseType: '住宿费',
        expenseDesc: '上海酒店住宿',
        amount: 700.00,
        taxAmount: 35.00,
        totalAmount: 735.00,
        baseAmount: 735.00,
        currency: 'CNY',
        exchangeRate: 1.0,
        invoiceType: 'special',
        invoiceNo: 'INV20260602002',
        invoiceCode: '3100890124',
        taxRate: 0.05,
      ),
    ],
  ),
];

final expensePageProvider = StateProvider<int>((ref) => 1);
final expenseDateStartProvider = StateProvider<DateTime?>((ref) => null);
final expenseDateEndProvider = StateProvider<DateTime?>((ref) => null);

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

final expenseListProvider = FutureProvider.autoDispose
    .family<List<ExpenseModel>, String>((ref, status) async {
      ref.watch(expensePageProvider);
      ref.watch(expenseDateStartProvider);
      ref.watch(expenseDateEndProvider);
      ref.watch(expenseRefreshProvider);
      // 模拟网络延迟，使骨架屏可见
      await Future.delayed(const Duration(milliseconds: 1000));

      List<ExpenseModel> list;
      if (status.isEmpty) {
        list = mockExpenses;
      } else if (status == 'unpaid') {
        list = mockExpenses
            .where((e) => e.status == 'approved' && e.paymentStatus == 'unpaid')
            .toList();
      } else if (status == 'paid') {
        list = mockExpenses
            .where((e) => e.status == 'approved' && e.paymentStatus == 'paid')
            .toList();
      } else {
        list = mockExpenses.where((e) => e.status == status).toList();
      }

      final dateStart = ref.read(expenseDateStartProvider);
      final dateEnd = ref.read(expenseDateEndProvider);
      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();
      }

      return list;
    });
