// SPDX-FileCopyrightText: 2026 FocusFlow contributors
// SPDX-License-Identifier: AGPL-3.0-only

/// Tests Backlog task detail drawer rendering and callbacks.
library;

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:scheduler_core/scheduler_core.dart';

import 'package:focus_flow_flutter/theme/focus_flow_theme.dart';
import 'package:focus_flow_flutter/widgets/backlog/backlog_task_drawer.dart';

/// Runs Backlog task drawer widget tests.
void main() {
  testWidgets('renders loaded detail sections and calls action callbacks', (
    tester,
  ) async {
    var closeCount = 0;
    var scheduleCount = 0;
    var breakUpCount = 0;
    var somedayCount = 0;
    var removeCount = 0;
    var addTagCount = 0;
    var viewDayCount = 0;

    await _pumpDrawer(
      tester,
      BacklogTaskDrawer(
        selectedTaskId: 'review-q3-budget',
        detail: _detail(),
        loading: false,
        errorMessage: null,
        actionMessage: null,
        onClose: () {
          closeCount += 1;
        },
        onSchedule: () {
          scheduleCount += 1;
        },
        onBreakUp: () {
          breakUpCount += 1;
        },
        onPushToSomeday: () {
          somedayCount += 1;
        },
        onRemove: () {
          removeCount += 1;
        },
        onProjectPressed: () {},
        onAddTagPressed: () {
          addTagCount += 1;
        },
        onViewDayPressed: () {
          viewDayCount += 1;
        },
      ),
    );

    expect(find.text('Review Q3 budget'), findsOneWidget);
    expect(find.text('Finance review and notes'), findsOneWidget);
    expect(find.text('Added 2h ago'), findsOneWidget);
    expect(find.text('Notes'), findsOneWidget);
    expect(find.text(_notes), findsOneWidget);
    expect(find.text('Project'), findsOneWidget);
    expect(find.text('Work'), findsOneWidget);
    expect(find.text('Tags'), findsOneWidget);
    expect(find.text('finance'), findsOneWidget);
    expect(find.text('review'), findsOneWidget);
    expect(find.text('Duration'), findsOneWidget);
    expect(find.text('60 min'), findsWidgets);
    expect(find.text('Reward'), findsOneWidget);
    expect(find.text('3/5'), findsOneWidget);
    expect(find.text('Satisfying'), findsOneWidget);
    expect(find.text('Difficulty'), findsOneWidget);
    expect(find.text('2/5'), findsOneWidget);
    expect(find.text('Easy'), findsOneWidget);
    expect(find.text('Suggested slots'), findsOneWidget);
    expect(find.text('Today'), findsNWidgets(2));
    expect(find.text('Tomorrow'), findsOneWidget);
    expect(find.text('2:30 PM - 3:30 PM | 60 min'), findsOneWidget);
    expect(find.text('Great Fit'), findsNWidgets(2));
    expect(find.text('Okay'), findsOneWidget);

    await tester.tap(
      find.byKey(const ValueKey('backlog-drawer-view-day-button')),
    );
    await tester.tap(
      find.byKey(const ValueKey('backlog-drawer-add-tag-button')),
    );
    await tester.tap(
      find.byKey(const ValueKey('backlog-drawer-schedule-button')),
    );
    await tester.tap(
      find.byKey(const ValueKey('backlog-drawer-break-up-button')),
    );
    await tester.tap(
      find.byKey(const ValueKey('backlog-drawer-push-someday-button')),
    );
    await tester.tap(
      find.byKey(const ValueKey('backlog-drawer-remove-button')),
    );
    await tester.tap(find.byKey(const ValueKey('backlog-drawer-close-button')));

    expect(viewDayCount, 1);
    expect(addTagCount, 1);
    expect(scheduleCount, 1);
    expect(breakUpCount, 1);
    expect(somedayCount, 1);
    expect(removeCount, 1);
    expect(closeCount, 1);
  });

  testWidgets('renders loading and error states without detail data', (
    tester,
  ) async {
    await _pumpDrawer(
      tester,
      BacklogTaskDrawer(
        selectedTaskId: 'task-1',
        detail: null,
        loading: true,
        errorMessage: null,
        actionMessage: null,
        onClose: () {},
        onSchedule: () {},
        onBreakUp: () {},
        onPushToSomeday: () {},
        onRemove: () {},
        onProjectPressed: () {},
        onAddTagPressed: () {},
        onViewDayPressed: () {},
      ),
    );

    expect(find.text('Loading task'), findsOneWidget);
    expect(find.text('Preparing task-1 details.'), findsOneWidget);

    await _pumpDrawer(
      tester,
      BacklogTaskDrawer(
        selectedTaskId: 'task-1',
        detail: null,
        loading: false,
        errorMessage: 'taskNotFound',
        actionMessage: null,
        onClose: () {},
        onSchedule: () {},
        onBreakUp: () {},
        onPushToSomeday: () {},
        onRemove: () {},
        onProjectPressed: () {},
        onAddTagPressed: () {},
        onViewDayPressed: () {},
      ),
    );

    expect(find.text('Could not load task details'), findsOneWidget);
    expect(find.text('taskNotFound'), findsOneWidget);
  });
}

/// Notes fixture text used by the drawer test.
const _notes =
    'Scan numbers and compare against plan. Note any variances and prepare talking points.';

/// Pumps [drawer] in a fixed-size dark FocusFlow test harness.
Future<void> _pumpDrawer(WidgetTester tester, Widget drawer) async {
  await tester.binding.setSurfaceSize(const Size(520, 860));
  addTearDown(() => tester.binding.setSurfaceSize(null));
  await tester.pumpWidget(
    MaterialApp(
      theme: FocusFlowTheme.dark(),
      home: Scaffold(
        body: Align(
          alignment: Alignment.centerRight,
          child: SizedBox(width: 466, height: 820, child: drawer),
        ),
      ),
    ),
  );
  await tester.pump();
}

/// Builds a loaded drawer detail fixture.
BacklogTaskDetailReadModel _detail() {
  return BacklogTaskDetailReadModel(
    item: BacklogBoardItemReadModel(
      taskId: 'review-q3-budget',
      title: 'Review Q3 budget',
      subtitle: 'Finance review and notes',
      projectId: 'work',
      projectName: 'Work',
      projectColorToken: 'project-work',
      durationMinutes: 60,
      priority: PriorityLevel.high,
      reward: RewardLevel.medium,
      difficulty: DifficultyLevel.easy,
      bucket: BacklogBoardBucket.needTimeBlock,
      bucketReason: 'Give this a protected block.',
      createdAt: DateTime.utc(2026, 7, 7, 19),
      updatedAt: DateTime.utc(2026, 7, 7, 19),
      childPreview: const [],
      tags: const ['finance', 'review'],
    ),
    notes: _notes,
    addedLabel: 'Added 2h ago',
    tags: const ['finance', 'review'],
    projectOptions: const [
      ProjectOptionReadModel(
        projectId: 'work',
        name: 'Work',
        colorToken: 'project-work',
        isArchived: false,
      ),
    ],
    suggestedSlots: const [
      SuggestedSlotReadModel(
        id: 'slot-1',
        localDateLabel: 'Today',
        startText: '2:30 PM',
        endText: '3:30 PM',
        durationMinutes: 60,
        fitLabel: 'Great Fit',
        fitSeverityToken: 'fit-great',
        isPrimary: true,
      ),
      SuggestedSlotReadModel(
        id: 'slot-2',
        localDateLabel: 'Today',
        startText: '6:00 PM',
        endText: '7:00 PM',
        durationMinutes: 60,
        fitLabel: 'Okay',
        fitSeverityToken: 'fit-okay',
        isPrimary: false,
      ),
      SuggestedSlotReadModel(
        id: 'slot-3',
        localDateLabel: 'Tomorrow',
        startText: '10:00 AM',
        endText: '11:00 AM',
        durationMinutes: 60,
        fitLabel: 'Great Fit',
        fitSeverityToken: 'fit-great',
        isPrimary: false,
      ),
    ],
  );
}
