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

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

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',
    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,
        totalAmount: 553.00,
      ),
      ExpenseDetailModel(
        id: 'det-002',
        expenseId: 'exp-001',
        expenseDate: DateTime(2026, 5, 19),
        expenseType: '住宿费',
        expenseDesc: '上海酒店住宿',
        amount: 800.00,
        totalAmount: 800.00,
      ),
    ],
    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-002',
    applicantName: '王五',
    deptId: 'dept-002',
    deptName: '技术部',
    expenseType: '办公用品',
    totalAmount: 1280.50,
    invoiceCount: 2,
    status: 'approved',
    createTime: DateTime(2026, 5, 15),
    updateTime: DateTime(2026, 5, 18),
    details: [
      ExpenseDetailModel(
        id: 'det-003',
        expenseId: 'exp-002',
        expenseDate: DateTime(2026, 5, 14),
        expenseType: '办公用品',
        expenseDesc: '打印纸、墨盒',
        amount: 1280.50,
        totalAmount: 1280.50,
      ),
    ],
    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',
    createTime: DateTime(2026, 5, 10),
    updateTime: DateTime(2026, 5, 12),
    details: [
      ExpenseDetailModel(
        id: 'det-004',
        expenseId: 'exp-003',
        expenseDate: DateTime(2026, 5, 9),
        expenseType: '招待费',
        expenseDesc: '客户晚宴',
        amount: 3600.00,
        totalAmount: 3600.00,
      ),
    ],
    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-001',
    applicantName: '张三',
    deptId: 'dept-001',
    deptName: '市场部',
    expenseType: '交通费',
    totalAmount: 890.00,
    invoiceCount: 1,
    status: 'pending',
    createTime: DateTime(2026, 5, 22),
    updateTime: DateTime(2026, 5, 22),
    details: [
      ExpenseDetailModel(
        id: 'det-005',
        expenseId: 'exp-004',
        expenseDate: DateTime(2026, 5, 21),
        expenseType: '交通费',
        expenseDesc: '市内打车',
        amount: 890.00,
        totalAmount: 890.00,
      ),
    ],
  ),
];

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

final expenseListProvider = FutureProvider<List<ExpenseModel>>((ref) async {
  ref.watch(expenseStatusFilterProvider);
  ref.watch(expensePageProvider);
  final status = ref.read(expenseStatusFilterProvider);
  if (status.isEmpty) return mockExpenses;
  return mockExpenses.where((e) => e.status == status).toList();
});
