import 'package:design_system/design_system.dart';
import 'package:flutter/material.dart';
import 'package:krow_domain/krow_domain.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;

  /// 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: ExperienceSkill.values.map((ExperienceSkill skill) {
            final bool isSelected = skills.contains(skill.value);
            // Dynamic translation access
            final String label = _getSkillLabel(skill);

            return UiChip(
              label: label,
              isSelected: isSelected,
              onTap: () => _toggleSkill(skill: skill.value),
              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: Industry.values.map((Industry industry) {
            final bool isSelected = industries.contains(industry.value);
            final String label = _getIndustryLabel(industry);

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

  String _getSkillLabel(ExperienceSkill skill) {
    switch (skill) {
      case ExperienceSkill.foodService:
        return t
            .staff_authentication
            .profile_setup_page
            .experience
            .skills
            .food_service;
      case ExperienceSkill.bartending:
        return t
            .staff_authentication
            .profile_setup_page
            .experience
            .skills
            .bartending;
      case ExperienceSkill.warehouse:
        return t
            .staff_authentication
            .profile_setup_page
            .experience
            .skills
            .warehouse;
      case ExperienceSkill.retail:
        return t
            .staff_authentication
            .profile_setup_page
            .experience
            .skills
            .retail;
      // Note: 'events' was removed from enum in favor of 'event_setup' or industry.
      // Using 'events' translation for eventSetup if available or fallback.
      case ExperienceSkill.eventSetup:
        return t
            .staff_authentication
            .profile_setup_page
            .experience
            .skills
            .events;
      case ExperienceSkill.customerService:
        return t
            .staff_authentication
            .profile_setup_page
            .experience
            .skills
            .customer_service;
      case ExperienceSkill.cleaning:
        return t
            .staff_authentication
            .profile_setup_page
            .experience
            .skills
            .cleaning;
      case ExperienceSkill.security:
        return t
            .staff_authentication
            .profile_setup_page
            .experience
            .skills
            .security;
      case ExperienceSkill.driving:
        return t
            .staff_authentication
            .profile_setup_page
            .experience
            .skills
            .driving;
      case ExperienceSkill.cooking:
        return t
            .staff_authentication
            .profile_setup_page
            .experience
            .skills
            .cooking;
      default:
        return skill.value;
    }
  }

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