import 'package:flutter/material.dart';
import '../../../../app/theme/app_colors.dart';
import '../../../../app/router.dart';
import '../../../../app/i18n/app_localizations.dart';

/// 发行方入驻审核流程
///
/// 步骤：企业信息 → 资质上传 → 联系人 → 提交审核 → 审核结果
/// 零保证金入驻：审核通过后给予初始低额度
enum OnboardingStep { companyInfo, documents, contactPerson, review, approved, rejected }

class OnboardingPage extends StatefulWidget {
  const OnboardingPage({super.key});

  @override
  State<OnboardingPage> createState() => _OnboardingPageState();
}

class _OnboardingPageState extends State<OnboardingPage> {
  OnboardingStep _currentStep = OnboardingStep.companyInfo;
  final _companyNameController = TextEditingController();
  final _licenseController = TextEditingController();
  final _contactController = TextEditingController();
  final _contactPhoneController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text(context.t('onboarding_title'))),
      body: Column(
        children: [
          // Step Indicator
          _buildStepIndicator(),
          const Divider(height: 1),

          // Step Content
          Expanded(
            child: SingleChildScrollView(
              padding: const EdgeInsets.all(24),
              child: _buildStepContent(),
            ),
          ),

          // Bottom Actions
          Padding(
            padding: const EdgeInsets.all(24),
            child: Row(
              children: [
                if (_currentStep.index > 0 && _currentStep.index < 3)
                  Expanded(
                    child: OutlinedButton(
                      onPressed: _goBack,
                      child: Text(context.t('prev_step')),
                    ),
                  ),
                if (_currentStep.index > 0 && _currentStep.index < 3) const SizedBox(width: 12),
                Expanded(
                  child: ElevatedButton(
                    onPressed: _goNext,
                    child: Text(_currentStep.index < 2 ? context.t('next') : context.t('onboarding_submit_review')),
                  ),
                ),
              ],
            ),
          ),
        ],
      ),
    );
  }

  Widget _buildStepIndicator() {
    final steps = [
      context.t('onboarding_step_company'),
      context.t('onboarding_step_documents'),
      context.t('onboarding_step_contact'),
      context.t('onboarding_step_review'),
    ];
    return Padding(
      padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 16),
      child: Row(
        children: List.generate(steps.length, (i) {
          final isActive = i <= _currentStep.index && _currentStep.index < 4;
          final isComplete = i < _currentStep.index;
          return Expanded(
            child: Row(
              children: [
                Container(
                  width: 28,
                  height: 28,
                  decoration: BoxDecoration(
                    color: isComplete
                        ? AppColors.success
                        : isActive
                            ? AppColors.primary
                            : AppColors.gray200,
                    shape: BoxShape.circle,
                  ),
                  child: Center(
                    child: isComplete
                        ? const Icon(Icons.check, color: Colors.white, size: 16)
                        : Text(
                            '${i + 1}',
                            style: TextStyle(
                              color: isActive ? Colors.white : AppColors.textTertiary,
                              fontSize: 13,
                              fontWeight: FontWeight.w600,
                            ),
                          ),
                  ),
                ),
                const SizedBox(width: 6),
                Expanded(
                  child: Text(
                    steps[i],
                    style: TextStyle(
                      fontSize: 12,
                      color: isActive ? AppColors.textPrimary : AppColors.textTertiary,
                      fontWeight: isActive ? FontWeight.w600 : FontWeight.w400,
                    ),
                    overflow: TextOverflow.ellipsis,
                  ),
                ),
              ],
            ),
          );
        }),
      ),
    );
  }

  Widget _buildStepContent() {
    switch (_currentStep) {
      case OnboardingStep.companyInfo:
        return _buildCompanyInfoStep();
      case OnboardingStep.documents:
        return _buildDocumentsStep();
      case OnboardingStep.contactPerson:
        return _buildContactStep();
      case OnboardingStep.review:
        return _buildReviewStep();
      case OnboardingStep.approved:
        return _buildApprovedStep();
      case OnboardingStep.rejected:
        return _buildRejectedStep();
    }
  }

  Widget _buildCompanyInfoStep() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(context.t('onboarding_company_title'), style: const TextStyle(fontSize: 20, fontWeight: FontWeight.w600)),
        const SizedBox(height: 8),
        Text(context.t('onboarding_company_subtitle'), style: const TextStyle(color: AppColors.textSecondary)),
        const SizedBox(height: 24),
        TextField(
          controller: _companyNameController,
          decoration: InputDecoration(labelText: context.t('onboarding_company_name'), hintText: context.t('onboarding_company_name_hint')),
        ),
        const SizedBox(height: 16),
        TextField(
          controller: _licenseController,
          decoration: InputDecoration(labelText: context.t('onboarding_credit_code'), hintText: context.t('onboarding_credit_code_hint')),
        ),
        const SizedBox(height: 16),
        DropdownButtonFormField<String>(
          decoration: InputDecoration(labelText: context.t('onboarding_company_type')),
          items: [
            DropdownMenuItem(value: 'restaurant', child: Text(context.t('onboarding_type_restaurant'))),
            DropdownMenuItem(value: 'retail', child: Text(context.t('onboarding_type_retail'))),
            DropdownMenuItem(value: 'entertainment', child: Text(context.t('onboarding_type_entertainment'))),
            DropdownMenuItem(value: 'other', child: Text(context.t('onboarding_type_other'))),
          ],
          onChanged: (_) {
            // TODO: Update selected company type
          },
        ),
        const SizedBox(height: 16),
        TextField(
          decoration: InputDecoration(labelText: context.t('onboarding_company_address'), hintText: context.t('onboarding_company_address_hint')),
        ),

        // AI合规助手
        const SizedBox(height: 24),
        Container(
          padding: const EdgeInsets.all(14),
          decoration: BoxDecoration(
            color: AppColors.primarySurface,
            borderRadius: BorderRadius.circular(12),
            border: Border.all(color: AppColors.primary.withValues(alpha: 0.15)),
          ),
          child: Row(
            children: [
              const Icon(Icons.auto_awesome_rounded, color: AppColors.primary, size: 20),
              const SizedBox(width: 10),
              Expanded(
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    Text(context.t('onboarding_ai_compliance'), style: const TextStyle(fontSize: 13, color: AppColors.primary, fontWeight: FontWeight.w600)),
                    const SizedBox(height: 2),
                    Text(context.t('onboarding_ai_compliance_desc'), style: const TextStyle(fontSize: 12, color: AppColors.textSecondary)),
                  ],
                ),
              ),
            ],
          ),
        ),
      ],
    );
  }

  Widget _buildDocumentsStep() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(context.t('onboarding_doc_title'), style: const TextStyle(fontSize: 20, fontWeight: FontWeight.w600)),
        const SizedBox(height: 8),
        Text(context.t('onboarding_doc_subtitle'), style: const TextStyle(color: AppColors.textSecondary)),
        const SizedBox(height: 24),
        _buildUploadArea(context.t('onboarding_doc_license'), Icons.business_rounded, true),
        const SizedBox(height: 16),
        _buildUploadArea(context.t('onboarding_doc_id'), Icons.badge_rounded, true),
        const SizedBox(height: 16),
        _buildUploadArea(context.t('onboarding_doc_cert'), Icons.verified_rounded, false),
      ],
    );
  }

  Widget _buildUploadArea(String title, IconData icon, bool required) {
    return Container(
      padding: const EdgeInsets.all(20),
      decoration: BoxDecoration(
        border: Border.all(color: AppColors.border, style: BorderStyle.solid),
        borderRadius: BorderRadius.circular(12),
      ),
      child: Column(
        children: [
          Icon(icon, size: 40, color: AppColors.textTertiary),
          const SizedBox(height: 8),
          Text(
            title,
            style: const TextStyle(fontSize: 14, fontWeight: FontWeight.w500),
          ),
          if (required)
            Text(context.t('onboarding_required'), style: const TextStyle(fontSize: 11, color: AppColors.error)),
          const SizedBox(height: 8),
          OutlinedButton.icon(
            onPressed: () {
              // TODO: Open file picker for document upload
            },
            icon: const Icon(Icons.upload_rounded, size: 18),
            label: Text(context.t('onboarding_upload')),
          ),
        ],
      ),
    );
  }

  Widget _buildContactStep() {
    return Column(
      crossAxisAlignment: CrossAxisAlignment.start,
      children: [
        Text(context.t('onboarding_contact_title'), style: const TextStyle(fontSize: 20, fontWeight: FontWeight.w600)),
        const SizedBox(height: 24),
        TextField(
          controller: _contactController,
          decoration: InputDecoration(labelText: context.t('onboarding_contact_name')),
        ),
        const SizedBox(height: 16),
        TextField(
          controller: _contactPhoneController,
          keyboardType: TextInputType.phone,
          decoration: InputDecoration(labelText: context.t('onboarding_contact_phone')),
        ),
        const SizedBox(height: 16),
        TextField(
          keyboardType: TextInputType.emailAddress,
          decoration: InputDecoration(labelText: context.t('onboarding_contact_email')),
        ),
        const SizedBox(height: 16),
        TextField(
          decoration: InputDecoration(labelText: context.t('onboarding_contact_role')),
        ),
      ],
    );
  }

  Widget _buildReviewStep() {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          const SizedBox(height: 40),
          Container(
            width: 80,
            height: 80,
            decoration: BoxDecoration(
              color: AppColors.warningLight,
              shape: BoxShape.circle,
            ),
            child: const Icon(Icons.hourglass_bottom_rounded, color: AppColors.warning, size: 40),
          ),
          const SizedBox(height: 24),
          Text(context.t('onboarding_review_title'), style: const TextStyle(fontSize: 24, fontWeight: FontWeight.w700)),
          const SizedBox(height: 8),
          Text(context.t('onboarding_review_desc'), style: const TextStyle(color: AppColors.textSecondary)),
          const SizedBox(height: 32),
          OutlinedButton(
            onPressed: () => Navigator.pop(context),
            child: Text(context.t('onboarding_back_login')),
          ),
        ],
      ),
    );
  }

  Widget _buildApprovedStep() {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          const SizedBox(height: 40),
          Container(
            width: 80,
            height: 80,
            decoration: const BoxDecoration(
              color: AppColors.successLight,
              shape: BoxShape.circle,
            ),
            child: const Icon(Icons.check_circle_rounded, color: AppColors.success, size: 40),
          ),
          const SizedBox(height: 24),
          Text(context.t('onboarding_approved_title'), style: const TextStyle(fontSize: 24, fontWeight: FontWeight.w700)),
          const SizedBox(height: 8),
          Text(context.t('onboarding_approved_desc'), style: const TextStyle(color: AppColors.textSecondary)),
          const SizedBox(height: 32),
          ElevatedButton(
            onPressed: () => Navigator.pushReplacementNamed(context, AppRouter.main),
            child: Text(context.t('onboarding_enter_console')),
          ),
        ],
      ),
    );
  }

  Widget _buildRejectedStep() {
    return Center(
      child: Column(
        mainAxisAlignment: MainAxisAlignment.center,
        children: [
          const SizedBox(height: 40),
          Container(
            width: 80,
            height: 80,
            decoration: const BoxDecoration(
              color: AppColors.errorLight,
              shape: BoxShape.circle,
            ),
            child: const Icon(Icons.cancel_rounded, color: AppColors.error, size: 40),
          ),
          const SizedBox(height: 24),
          Text(context.t('onboarding_rejected_title'), style: const TextStyle(fontSize: 24, fontWeight: FontWeight.w700)),
          const SizedBox(height: 8),
          Text(context.t('onboarding_rejected_desc'), style: const TextStyle(color: AppColors.textSecondary)),
          const SizedBox(height: 32),
          ElevatedButton(
            onPressed: () => setState(() => _currentStep = OnboardingStep.documents),
            child: Text(context.t('onboarding_resubmit')),
          ),
        ],
      ),
    );
  }

  void _goBack() {
    if (_currentStep.index > 0) {
      setState(() => _currentStep = OnboardingStep.values[_currentStep.index - 1]);
    }
  }

  void _goNext() {
    if (_currentStep.index < 2) {
      setState(() => _currentStep = OnboardingStep.values[_currentStep.index + 1]);
    } else if (_currentStep == OnboardingStep.contactPerson) {
      setState(() => _currentStep = OnboardingStep.review);
    }
  }
}
