import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:lucide_icons/lucide_icons.dart';
import 'package:package_info_plus/package_info_plus.dart';
import 'package:flutter/services.dart';
import '../../screens/dev_menu_screen.dart';
import '../../providers/theme_provider.dart';
import '../../theme/app_colors.dart';
import '../../constants/app_constants.dart';

class OtherSettingsSection extends ConsumerStatefulWidget {
  final bool showBusinessMode;
  final String title;

  const OtherSettingsSection({
    super.key,
    this.showBusinessMode = true,
    this.title = 'データ・その他',
  });

  @override
  ConsumerState<OtherSettingsSection> createState() => _OtherSettingsSectionState();
}

class _OtherSettingsSectionState extends ConsumerState<OtherSettingsSection> {
  String _appVersion = 'Loading...';
  int _devTapCount = 0;

  @override
  void initState() {
    super.initState();
    _loadAppVersion();
  }

  Future<void> _loadAppVersion() async {
    final packageInfo = await PackageInfo.fromPlatform();
    if (mounted) {
      setState(() {
        _appVersion = 'v${packageInfo.version}+${packageInfo.buildNumber}';
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    final userProfile = ref.watch(userProfileProvider);
    final appColors = Theme.of(context).extension<AppColors>()!;

    return Column(
      children: [
        _buildSectionHeader(context, widget.title, LucideIcons.database),
        Card(
          color: appColors.surfaceSubtle,
          child: Column(
            children: [
              if (widget.showBusinessMode) ...[
                ListTile(
                  leading: Icon(LucideIcons.store, color: appColors.warning),
                  title: Text('ビジネスモード (Beta)', style: TextStyle(color: appColors.textPrimary)),
                  subtitle: Text('お品書き作成機能など', style: TextStyle(color: appColors.textSecondary)),
                  trailing: Switch(
                    value: userProfile.isBusinessMode,
                    onChanged: (val) => ref.read(userProfileProvider.notifier).toggleBusinessMode(),
                    activeThumbColor: appColors.warning,
                    inactiveThumbColor: appColors.iconSubtle,
                    inactiveTrackColor: appColors.divider,
                  ),
                ),
                Divider(height: 1, color: appColors.divider),
              ],
               ListTile(
                 leading: Icon(LucideIcons.info, color: appColors.iconDefault),
                 title: Text('アプリバージョン', style: TextStyle(color: appColors.textPrimary)),
                 subtitle: Text(_appVersion, style: TextStyle(color: appColors.textSecondary)),
                 onTap: kReleaseMode ? null : () {
                   setState(() {
                     _devTapCount++;
                     if (_devTapCount >= AppConstants.devModeTapCount) {
                       _devTapCount = 0;
                       HapticFeedback.heavyImpact();
                       Navigator.push(
                         context,
                         MaterialPageRoute(builder: (context) => const DevMenuScreen()),
                       );
                     } else {
                       HapticFeedback.lightImpact();
                     }
                   });
                 },
               ),
            ],
          ),
        ),
      ],
    );
  }

  Widget _buildSectionHeader(BuildContext context, String title, IconData icon) {
    final appColors = Theme.of(context).extension<AppColors>()!;
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 4),
      child: Row(
        children: [
          Icon(icon, size: 20, color: appColors.iconDefault),
          const SizedBox(width: 8),
          Text(
            title,
            style: Theme.of(context).textTheme.titleMedium?.copyWith(
              fontWeight: FontWeight.bold,
              color: appColors.textPrimary,
            ),
          ),
        ],
      ),
    );
  }
}
