import 'package:flutter_test/flutter_test.dart';

import 'package:superport_v2/features/approvals/domain/entities/approval_draft.dart';

void main() {
  group('ApprovalDraftSaveInput', () {
    test('metadata에 상태 ID를 병합한다', () {
      final input = ApprovalDraftSaveInput(
        requesterId: 99,
        statusId: 7,
        metadata: const {'foo': 'bar'},
        steps: [ApprovalDraftStep(stepOrder: 1, approverId: 2)],
      );

      final json = input.toJson();
      expect(json['metadata'], isA<Map<String, dynamic>>());
      final metadata = json['metadata'] as Map<String, dynamic>;
      expect(metadata['_client_state'], isA<Map<String, dynamic>>());
      final client = metadata['_client_state'] as Map<String, dynamic>;
      expect(client['status_id'], 7);
      expect(metadata['foo'], 'bar');
    });
  });

  group('ApprovalDraftDetail', () {
    test('toSubmissionInput이 metadata에서 상태 ID를 복원한다', () {
      final detail = ApprovalDraftDetail(
        id: 1,
        requesterId: 10,
        savedAt: DateTime.utc(2025, 1, 1),
        payload: ApprovalDraftPayload(
          templateId: 5,
          title: '임시 저장',
          metadata: const {
            '_client_state': {'status_id': 3},
            'note': 'memo',
          },
          steps: [ApprovalDraftStep(stepOrder: 1, approverId: 42)],
        ),
      );

      final submission = detail.toSubmissionInput(defaultStatusId: 1);

      expect(submission.requesterId, 10);
      expect(submission.statusId, 3);
      expect(submission.templateId, 5);
      expect(submission.metadata, isA<Map<String, dynamic>>());
      final metadata = submission.metadata!;
      expect(metadata.containsKey('_client_state'), isFalse);
      expect(metadata['note'], 'memo');
      expect(submission.steps, hasLength(1));
      expect(submission.steps.first.approverId, 42);
    });
  });
}
