import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart';
import 'package:staff_authentication/src/presentation/widgets/common/section_title_subtitle.dart';
import 'package:staff_authentication/staff_authentication.dart';

/// A widget for setting up skills and preferred industries.
class ProfileSetupExperience extends StatelessWidget {
  /// The list of selected skills.
  final List<String> skills;

  /// The list of selected industries.
  final List<String> industries;

  /// Callback for when skills change.
  final ValueChanged<List<String>> onSkillsChanged;

  /// Callback for when industries change.
  final ValueChanged<List<String>> onIndustriesChanged;

  static const List<String> _allSkillKeys = <String>[
    'food_service',
    'bartending',
    'warehouse',
    'retail',
    'events',
    'customer_service',
    'cleaning',
    'security',
    'driving',
    'cooking',
  ];

  static const List<String> _allIndustryKeys = <String>[
    'hospitality',
    'food_service',
    'warehouse',
    'events',
    'retail',
    'healthcare',
  ];

  /// Creates a [ProfileSetupExperience] widget.
  const ProfileSetupExperience({
    super.key,
    required this.skills,
    required this.industries,
    required this.onSkillsChanged,
    required this.onIndustriesChanged,
  });

  /// Toggles a skill.
  void _toggleSkill({required String skill}) {
    final List<String> updatedList = List<String>.from(skills);
    if (updatedList.contains(skill)) {
      updatedList.remove(skill);
    } else {
      updatedList.add(skill);
    }
    onSkillsChanged(updatedList);
  }

  /// Toggles an industry.
  void _toggleIndustry({required String industry}) {
    final List<String> updatedList = List<String>.from(industries);
    if (updatedList.contains(industry)) {
      updatedList.remove(industry);
    } else {
      updatedList.add(industry);
    }
    onIndustriesChanged(updatedList);
  }

  @override
  /// Builds the experience setup step UI.
  Widget build(BuildContext context) {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: <Widget>[
        SectionTitleSubtitle(
          title: t.staff_authentication.profile_setup_page.experience.title,
          subtitle:
              t.staff_authentication.profile_setup_page.experience.subtitle,
        ),
        const SizedBox(height: UiConstants.space8),

        // Skills
        Text(
          t.staff_authentication.profile_setup_page.experience.skills_label,
          style: UiTypography.body2m,
        ),
        const SizedBox(height: UiConstants.space3),
        Wrap(
          spacing: UiConstants.space2,
          runSpacing: UiConstants.space2,
          children: _allSkillKeys.map((String key) {
            final bool isSelected = skills.contains(key);
            // Dynamic translation access
            final String label = _getSkillLabel(key);

            return UiChip(
              label: label,
              isSelected: isSelected,
              onTap: () => _toggleSkill(skill: key),
              leadingIcon: isSelected ? UiIcons.check : null,
              variant: UiChipVariant.primary,
            );
          }).toList(),
        ),

        const SizedBox(height: UiConstants.space8),

        // Industries
        Text(
          t.staff_authentication.profile_setup_page.experience.industries_label,
          style: UiTypography.body2m,
        ),
        const SizedBox(height: UiConstants.space3),
        Wrap(
          spacing: UiConstants.space2,
          runSpacing: UiConstants.space2,
          children: _allIndustryKeys.map((String key) {
            final bool isSelected = industries.contains(key);
            final String label = _getIndustryLabel(key);

            return UiChip(
              label: label,
              isSelected: isSelected,
              onTap: () => _toggleIndustry(industry: key),
              leadingIcon: isSelected ? UiIcons.check : null,
              variant: isSelected
                  ? UiChipVariant.accent
                  : UiChipVariant.primary,
            );
          }).toList(),
        ),
      ],
    );
  }

  String _getSkillLabel(String key) {
    switch (key) {
      case 'food_service':
        return t
            .staff_authentication
            .profile_setup_page
            .experience
            .skills
            .food_service;
      case 'bartending':
        return t
            .staff_authentication
            .profile_setup_page
            .experience
            .skills
            .bartending;
      case 'warehouse':
        return t
            .staff_authentication
            .profile_setup_page
            .experience
            .skills
            .warehouse;
      case 'retail':
        return t
            .staff_authentication
            .profile_setup_page
            .experience
            .skills
            .retail;
      case 'events':
        return t
            .staff_authentication
            .profile_setup_page
            .experience
            .skills
            .events;
      case 'customer_service':
        return t
            .staff_authentication
            .profile_setup_page
            .experience
            .skills
            .customer_service;
      case 'cleaning':
        return t
            .staff_authentication
            .profile_setup_page
            .experience
            .skills
            .cleaning;
      case 'security':
        return t
            .staff_authentication
            .profile_setup_page
            .experience
            .skills
            .security;
      case 'driving':
        return t
            .staff_authentication
            .profile_setup_page
            .experience
            .skills
            .driving;
      case 'cooking':
        return t
            .staff_authentication
            .profile_setup_page
            .experience
            .skills
            .cooking;
      default:
        return key;
    }
  }

  String _getIndustryLabel(String key) {
    switch (key) {
      case 'hospitality':
        return t
            .staff_authentication
            .profile_setup_page
            .experience
            .industries
            .hospitality;
      case 'food_service':
        return t
            .staff_authentication
            .profile_setup_page
            .experience
            .industries
            .food_service;
      case 'warehouse':
        return t
            .staff_authentication
            .profile_setup_page
            .experience
            .industries
            .warehouse;
      case 'events':
        return t
            .staff_authentication
            .profile_setup_page
            .experience
            .industries
            .events;
      case 'retail':
        return t
            .staff_authentication
            .profile_setup_page
            .experience
            .industries
            .retail;
      case 'healthcare':
        return t
            .staff_authentication
            .profile_setup_page
            .experience
            .industries
            .healthcare;
      default:
        return key;
    }
  }
}
