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';

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 isDark = Theme.of(context).brightness == Brightness.dark;

    return Column(
      children: [
        _buildSectionHeader(context, widget.title, LucideIcons.database),
        Card(
          child: Column(
            children: [
              if (widget.showBusinessMode) ...[
                ListTile(
                  leading: Icon(LucideIcons.store, color: isDark ? Colors.orange[300] : Colors.orange),
                  title: const Text('ビジネスモード (Beta)'),
                  subtitle: const Text('お品書き作成機能など'),
                  trailing: Switch(
                    value: userProfile.isBusinessMode,
                    onChanged: (val) => ref.read(userProfileProvider.notifier).toggleBusinessMode(),
                    activeThumbColor: Colors.orange,
                  ),
                ),
                const Divider(height: 1),
              ],
               ListTile(
                 leading: Icon(LucideIcons.info, color: isDark ? Colors.grey[400] : null),
                 title: const Text('アプリバージョン'),
                 subtitle: Text(_appVersion),
                 onTap: () {
                   setState(() {
                     _devTapCount++;
                     if (_devTapCount >= 5) {
                       _devTapCount = 0;
                       HapticFeedback.heavyImpact();
                       Navigator.push(
                         context,
                         MaterialPageRoute(builder: (context) => const DevMenuScreen()),
                       );
                     } else {
                        // Optional: Small feedback
                        HapticFeedback.lightImpact();
                     }
                   });
                 },
               ),
            ],
          ),
        ),
      ],
    );
  }

  Widget _buildSectionHeader(BuildContext context, String title, IconData icon) {
    final isDark = Theme.of(context).brightness == Brightness.dark;
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 4),
      child: Row(
        children: [
          Icon(icon, size: 20, color: isDark ? Colors.orange[300] : Theme.of(context).primaryColor),
          const SizedBox(width: 8),
          Text(
            title,
            style: Theme.of(context).textTheme.titleMedium?.copyWith(
              fontWeight: FontWeight.bold,
              color: isDark ? Colors.grey[300] : Theme.of(context).primaryColor,
            ),
          ),
        ],
      ),
    );
  }
}
