/// Remote Data Source: Points Record
///
/// Handles API communication for points records.
library;

import 'package:worker/features/loyalty/domain/entities/points_record.dart';

/// Points Record Remote Data Source Interface
abstract class PointsRecordRemoteDataSource {
  /// Get all points records for current user
  Future<List<PointsRecord>> getPointsRecords();

  /// Get single points record by ID
  Future<PointsRecord> getPointsRecordById(String recordId);

  /// Submit new points record
  Future<PointsRecord> submitPointsRecord(PointsRecord record);
}

/// Points Record Remote Data Source Implementation (Mock)
class PointsRecordRemoteDataSourceImpl
    implements PointsRecordRemoteDataSource {
  @override
  Future<List<PointsRecord>> getPointsRecords() async {
    await Future<void>.delayed(const Duration(milliseconds: 500));

    return [
      PointsRecord(
        recordId: 'PRR001',
        userId: 'user123',
        invoiceNumber: 'INV-VG-001',
        storeName: 'Công ty TNHH Vingroup',
        transactionDate: DateTime(2025, 11, 15),
        invoiceAmount: 2500000,
        notes: 'Gạch granite cao cấp cho khu vực lobby và hành lang',
        attachments: const [
          'https://example.com/invoice1.jpg',
          'https://example.com/invoice2.jpg',
        ],
        status: PointsStatus.approved,
        pointsEarned: 250,
        submittedAt: DateTime(2025, 11, 15, 10, 0),
        processedAt: DateTime(2025, 11, 20, 14, 30),
        processedBy: 'admin001',
      ),
      PointsRecord(
        recordId: 'PRR002',
        userId: 'user123',
        invoiceNumber: 'INV-BTX-002',
        storeName: 'Tập đoàn Bitexco',
        transactionDate: DateTime(2025, 11, 25),
        invoiceAmount: 1250000,
        notes: 'Gạch porcelain 80x80 cho sảnh chính và khu mua sắm',
        attachments: const [
          'https://example.com/invoice3.jpg',
        ],
        status: PointsStatus.pending,
        submittedAt: DateTime(2025, 11, 25, 9, 15),
      ),
      PointsRecord(
        recordId: 'PRR003',
        userId: 'user123',
        invoiceNumber: 'INV-ABC-003',
        storeName: 'Công ty TNHH ABC Manufacturing',
        transactionDate: DateTime(2025, 11, 20),
        invoiceAmount: 4200000,
        notes: 'Gạch chống trơn cho khu vực sản xuất và kho bãi',
        attachments: const [
          'https://example.com/invoice4.jpg',
          'https://example.com/invoice5.jpg',
        ],
        status: PointsStatus.rejected,
        rejectReason: 'Hình ảnh minh chứng không hợp lệ',
        submittedAt: DateTime(2025, 11, 20, 11, 0),
        processedAt: DateTime(2025, 11, 28, 16, 45),
        processedBy: 'admin002',
      ),
      PointsRecord(
        recordId: 'PRR004',
        userId: 'user123',
        invoiceNumber: 'INV-ECO-004',
        storeName: 'Ecopark Group',
        transactionDate: DateTime(2025, 10, 10),
        invoiceAmount: 3700000,
        notes: 'Gạch ceramic vân gỗ cho khu vực phòng khách và sân vườn',
        attachments: const [
          'https://example.com/invoice6.jpg',
        ],
        status: PointsStatus.approved,
        pointsEarned: 370,
        submittedAt: DateTime(2025, 10, 10, 8, 30),
        processedAt: DateTime(2025, 10, 15, 10, 20),
        processedBy: 'admin001',
      ),
      PointsRecord(
        recordId: 'PRR005',
        userId: 'user123',
        invoiceNumber: 'INV-DMD-005',
        storeName: 'Diamond Hospitality Group',
        transactionDate: DateTime(2025, 12, 1),
        invoiceAmount: 8600000,
        notes: 'Gạch marble tự nhiên cho lobby và phòng suite',
        attachments: const [
          'https://example.com/invoice7.jpg',
          'https://example.com/invoice8.jpg',
          'https://example.com/invoice9.jpg',
        ],
        status: PointsStatus.pending,
        submittedAt: DateTime(2025, 12, 1, 13, 0),
      ),
    ];
  }

  @override
  Future<PointsRecord> getPointsRecordById(String recordId) async {
    await Future<void>.delayed(const Duration(milliseconds: 300));

    final records = await getPointsRecords();
    return records.firstWhere(
      (record) => record.recordId == recordId,
      orElse: () => throw Exception('Points record not found'),
    );
  }

  @override
  Future<PointsRecord> submitPointsRecord(PointsRecord record) async {
    await Future<void>.delayed(const Duration(milliseconds: 800));

    // Simulate successful submission
    return record.copyWith(
      recordId: 'PRR${DateTime.now().millisecondsSinceEpoch}',
      status: PointsStatus.pending,
      submittedAt: DateTime.now(),
    );
  }
}
