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

/// Defines Backlog board visual tokens for the FocusFlow Flutter UI.
library;

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

import 'focus_flow_tokens.dart';

/// Maps Backlog board read-model tokens to Flutter colors and icons.
abstract final class BacklogBoardVisualTokens {
  /// Accent color for the Do Next bucket.
  static const doNextAccent = Color(0xFF24E65A);

  /// Accent color for the Need Time Block bucket.
  static const needTimeBlockAccent = Color(0xFFFFA51F);

  /// Accent color for the Break Up First bucket.
  static const breakUpFirstAccent = Color(0xFFB45CFF);

  /// Accent color for the Not Now bucket.
  static const notNowAccent = Color(0xFF35BFFF);

  /// Red flag color for high-priority Backlog cards.
  static const priorityHigh = Color(0xFFFF3F5B);

  /// Yellow flag color for medium-priority Backlog cards.
  static const priorityMedium = Color(0xFFFFD447);

  /// Green flag color for low-priority Backlog cards.
  static const priorityLow = Color(0xFF4BE064);

  /// Project dot color for Home.
  static const projectHome = Color(0xFF4D86FF);

  /// Project dot color for Work.
  static const projectWork = Color(0xFFFF7A1A);

  /// Project dot color for Personal.
  static const projectPersonal = Color(0xFFB45CFF);

  /// Project dot color for Learning.
  static const projectLearning = Color(0xFF38BDF8);

  /// Project dot color for Side Project.
  static const projectSideProject = Color(0xFF2F6BFF);

  /// Resolves a Backlog column accent token to a display color.
  static Color accentTokenColor(
    String token, {
    BacklogBoardBucket? fallbackBucket,
  }) {
    return switch (token) {
      'backlog-do-next' => doNextAccent,
      'backlog-time-block' => needTimeBlockAccent,
      'backlog-break-up' => breakUpFirstAccent,
      'backlog-not-now' => notNowAccent,
      'green' => doNextAccent,
      'yellow' => needTimeBlockAccent,
      'purple' => breakUpFirstAccent,
      'blue' => notNowAccent,
      _ =>
        fallbackBucket == null
            ? FocusFlowTokens.textMuted
            : bucketAccent(fallbackBucket),
    };
  }

  /// Resolves a board bucket to its accent color.
  static Color bucketAccent(BacklogBoardBucket bucket) {
    return switch (bucket) {
      BacklogBoardBucket.doNext => doNextAccent,
      BacklogBoardBucket.needTimeBlock => needTimeBlockAccent,
      BacklogBoardBucket.breakUpFirst => breakUpFirstAccent,
      BacklogBoardBucket.notNow => notNowAccent,
    };
  }

  /// Resolves a board bucket to its header icon.
  static IconData bucketIcon(BacklogBoardBucket bucket) {
    return switch (bucket) {
      BacklogBoardBucket.doNext => Icons.check_circle_outline,
      BacklogBoardBucket.needTimeBlock => Icons.schedule,
      BacklogBoardBucket.breakUpFirst => Icons.adjust,
      BacklogBoardBucket.notNow => Icons.cloud_outlined,
    };
  }

  /// Resolves a task priority to a flag color.
  static Color priorityColor(PriorityLevel? priority) {
    return switch (priority) {
      PriorityLevel.veryHigh || PriorityLevel.high => priorityHigh,
      PriorityLevel.medium => priorityMedium,
      PriorityLevel.low || PriorityLevel.veryLow => priorityLow,
      null => FocusFlowTokens.textFaint,
    };
  }

  /// Resolves a priority distribution color token to a flag color.
  static Color priorityColorToken(String? token) {
    return switch (token) {
      'priority-veryHigh' || 'priority-high' => priorityHigh,
      'priority-medium' => priorityMedium,
      'priority-low' || 'priority-veryLow' => priorityLow,
      _ => FocusFlowTokens.textFaint,
    };
  }

  /// Resolves a task priority to a compact card label.
  static String priorityLabel(PriorityLevel? priority) {
    return switch (priority) {
      PriorityLevel.veryHigh || PriorityLevel.high => 'High',
      PriorityLevel.medium => 'Med',
      PriorityLevel.low || PriorityLevel.veryLow => 'Low',
      null => 'None',
    };
  }

  /// Resolves a project color token to the Backlog board dot color.
  static Color projectColor(String token) {
    return switch (token) {
      'project-home' || 'home' => projectHome,
      'project-work' || 'work' => projectWork,
      'project-personal' || 'personal' => projectPersonal,
      'project-learning' || 'learning' => projectLearning,
      'project-side-project' || 'side-project' => projectSideProject,
      _ => FocusFlowTokens.textMuted,
    };
  }

  /// Resolves a duration distribution id to a chart segment color.
  static Color durationColor(String id) {
    return switch (id) {
      '0-30' => priorityLow,
      '30-60' => notNowAccent,
      '60-120' => breakUpFirstAccent,
      '120+' => FocusFlowTokens.accentMagenta,
      _ => FocusFlowTokens.textFaint,
    };
  }

  /// Resolves a reward level to the number of solid reward marks to render.
  static int rewardCount(RewardLevel reward) {
    return switch (reward) {
      RewardLevel.notSet => 0,
      RewardLevel.veryLow => 1,
      RewardLevel.low => 2,
      RewardLevel.medium => 3,
      RewardLevel.high => 4,
      RewardLevel.veryHigh => 5,
    };
  }

  /// Resolves a Backlog tag chip background color for drawer surfaces.
  static Color tagChipBackground(String label) {
    return switch (label.toLowerCase()) {
      'someday' => notNowAccent.withValues(alpha: 0.16),
      _ => FocusFlowTokens.elevatedPanel,
    };
  }

  /// Resolves a Backlog tag chip foreground color for drawer surfaces.
  static Color tagChipForeground(String label) {
    return switch (label.toLowerCase()) {
      'someday' => notNowAccent,
      _ => FocusFlowTokens.textMuted,
    };
  }
}
