import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'outing_log_comment.dart';
import 'outing_log_model.dart';

final mockOutingLogs = <OutingLogModel>[
  OutingLogModel(
    id: 'log-001',
    visitNo: 'VST-20260520-001',
    salespersonId: 'u-001',
    salespersonName: '张三',
    deptId: 'dept-001',
    deptName: '市场部',
    customerId: 'c-001',
    customerName: '华软科技',
    contactId: 'ct-001',
    contactName: '赵经理',
    contactPhone: '13800138001',
    checkInLongitude: 113.9532,
    checkInLatitude: 22.5431,
    gpsAccuracy: 15.0,
    checkInAddress: '深圳市南山区科技园南路88号',
    visitSummary: '向客户展示了公司最新产品功能，客户对数据看板功能比较感兴趣，约定下周安排试用。',
    nextPlan: '下周安排产品试用，准备定制化方案',
    visitPhotos: ['photo_placeholder_1', 'photo_placeholder_2'],
    status: 'completed',
    comments: [
      OutingLogComment(
        id: 'cmt-001',
        logId: 'log-001',
        commenterId: 'u-mgr',
        commenterName: '王经理',
        commenterPosition: '销售总监',
        ratingStars: 4,
        commentText: '本次拜访准备充分，方案汇报专业到位。建议后续跟进加快节奏，争取本月内签单。',
        createTime: DateTime(2026, 5, 20, 16, 30),
      ),
    ],
    createTime: DateTime(2026, 5, 20, 9, 0),
    updateTime: DateTime(2026, 5, 20, 16, 30),
    lastViewedTime: DateTime(2026, 5, 20, 14, 0),
  ),
  OutingLogModel(
    id: 'log-002',
    visitNo: 'VST-20260522-001',
    salespersonId: 'u-002',
    salespersonName: '王五',
    deptId: 'dept-002',
    deptName: '技术部',
    customerName: '云创数据',
    checkInLongitude: 113.3251,
    checkInLatitude: 23.1285,
    gpsAccuracy: 8.0,
    checkInAddress: '广州市天河区软件路15号',
    visitSummary: '与客户技术团队确认了API对接方案，明确了数据字段映射关系，预计下周开始联调。',
    nextPlan: '下周开始联调，准备技术接口文档',
    visitPhotos: ['photo_placeholder_3'],
    status: 'completed',
    createTime: DateTime(2026, 5, 22, 14, 0),
    updateTime: DateTime(2026, 5, 22, 16, 0),
  ),
  OutingLogModel(
    id: 'log-003',
    visitNo: 'VST-20260525-001',
    salespersonId: 'u-001',
    salespersonName: '张三',
    deptId: 'dept-001',
    deptName: '市场部',
    customerName: '数据引力',
    contactName: '孙总',
    contactPhone: '13800138003',
    checkInLongitude: 116.4803,
    checkInLatitude: 39.9965,
    gpsAccuracy: 25.0,
    checkInAddress: '北京市朝阳区望京SOHO',
    visitSummary: '就合同关键条款达成一致，客户同意签约，预计下月初正式签订合同。',
    nextPlan: '准备合同文本，预约法务审核',
    visitPhotos: [
      'photo_placeholder_4',
      'photo_placeholder_5',
      'photo_placeholder_6',
    ],
    status: 'completed',
    comments: [
      OutingLogComment(
        id: 'cmt-002',
        logId: 'log-003',
        commenterId: 'u-mgr',
        commenterName: '王经理',
        commenterPosition: '销售总监',
        ratingStars: 5,
        commentText: '优秀的谈判成果！继续保持这个势头。',
        createTime: DateTime(2026, 5, 25, 14, 0),
      ),
    ],
    createTime: DateTime(2026, 5, 25, 10, 0),
    updateTime: DateTime(2026, 5, 25, 14, 0),
    lastViewedTime: DateTime(2026, 5, 25, 12, 0),
  ),
  OutingLogModel(
    id: 'log-004',
    visitNo: 'VST-20260528-001',
    salespersonId: 'u-001',
    salespersonName: '张三',
    deptId: 'dept-001',
    deptName: '市场部',
    customerName: '天诚科技',
    gpsAccuracy: 120.0,
    checkInAddress: '深圳市福田区深南大道100号',
    visitSummary: '初步接触客户，了解客户需求。客户对数据可视化方案有兴趣，但预算有限。后续需要准备更具性价比的方案。',
    nextPlan: '准备性价比方案，下周再次拜访',
    visitPhotos: ['photo_placeholder_7'],
    status: 'completed',
    createTime: DateTime(2026, 5, 28, 9, 30),
    updateTime: DateTime(2026, 5, 28, 11, 0),
  ),
  OutingLogModel(
    id: 'log-005',
    visitNo: 'VST-20260601-001',
    salespersonId: 'u-001',
    salespersonName: '张三',
    deptId: 'dept-001',
    deptName: '市场部',
    customerName: '博思软件',
    checkInAddress: '广州市番禺区汉溪大道',
    gpsAccuracy: 10.0,
    visitSummary: '为客户做产品演示',
    nextPlan: '',
    visitPhotos: [],
    status: 'draft',
    createTime: DateTime(2026, 6, 1, 10, 0),
    updateTime: DateTime(2026, 6, 1, 10, 0),
  ),
  OutingLogModel(
    id: 'log-006',
    visitNo: 'VST-20260603-001',
    salespersonId: 'u-003',
    salespersonName: '李四',
    deptId: 'dept-003',
    deptName: '销售部',
    customerName: '智云科技',
    checkInAddress: '深圳市宝安区创业路1号',
    gpsAccuracy: 18.0,
    visitSummary: '拜访客户做需求调研，了解客户数字化转型需求，初步推荐了三套解决方案。',
    nextPlan: '准备详细解决方案，约下周再次交流',
    visitPhotos: ['photo_placeholder_8', 'photo_placeholder_9'],
    status: 'draft',
    createTime: DateTime(2026, 6, 3, 14, 0),
    updateTime: DateTime(2026, 6, 3, 16, 0),
  ),
];

final outingLogTabProvider = StateProvider<int>((ref) => 0);
final outingLogScopeProvider = StateProvider<int>((ref) => 0);

final outingLogDateStartProvider = StateProvider<DateTime?>((ref) => null);
final outingLogDateEndProvider = StateProvider<DateTime?>((ref) => null);

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

final filteredOutingLogsProvider = FutureProvider
    .family<List<OutingLogModel>, int>((ref, tabIndex) async {
      ref.watch(outingLogDateStartProvider);
      ref.watch(outingLogDateEndProvider);
      ref.watch(outingLogRefreshProvider);
      // 模拟网络延迟，使骨架屏可见
      await Future.delayed(const Duration(milliseconds: 1000));
      var list = mockOutingLogs;
      if (tabIndex == 1) {
        list = list.where((e) => e.status == 'draft').toList();
      } else if (tabIndex == 2) {
        list = list.where((e) => e.status == 'completed').toList();
      }

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