import 'package:digiplug/bat_theme/bat_theme.dart';
import 'package:digiplug/core/navigation/navigator.dart';
import 'package:digiplug/data/repositories/auth_repository.dart';
import 'package:digiplug/providers/home_data_provider.dart';
import 'package:digiplug/shared/widgets/confirmation_dialog.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:provider/provider.dart';

class ProfileScreen extends StatelessWidget {
  const ProfileScreen({super.key});

  // Fungsi untuk menangani proses logout
  void _handleLogout(BuildContext context) async {
    final confirmed = await showConfirmationDialog(
      context: context,
      title: 'Logout',
      content: 'Apakah Anda yakin ingin keluar dari akun Anda?',
      confirmText: 'Ya, Logout',
    );

    if (confirmed && context.mounted) {
      final authRepo = context.read<AuthRepository>();
      await authRepo.logout();
      AppNavigator.pushNamedAndClear(loginRoute);
    }
  }

  @override
  Widget build(BuildContext context) {
    final theme = BatThemeData.of(context);
    final deviceCount = context.watch<HomeDataProvider>().devices.length;

    return Scaffold(
      backgroundColor: theme.colors.background,
      body: SingleChildScrollView(
        child: Padding(
          padding: EdgeInsets.symmetric(horizontal: 24.w),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.center,
            children: [
              SizedBox(height: 48.h),
              // Informasi Pengguna
              const CircleAvatar(
                radius: 48.0,
                backgroundImage: AssetImage('assets/images/profile.png'),
              ),
              SizedBox(height: 16.h),
              Text(
                'Hensen',
                style: theme.typography.headline4
                    .copyWith(color: theme.colors.tertiary),
              ),
              Text(
                'hensen@example.com',
                style: theme.typography.bodyCopy
                    .copyWith(color: theme.colors.tertiary.withOpacity(0.6)),
              ),
              SizedBox(height: 32.h),

              // Ringkasan
              Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: [
                  _buildSummaryCard(
                      theme, Icons.bolt_sharp, 'Power', '5.7 kWh'),
                  SizedBox(width: 16.w),
                  _buildSummaryCard(theme, Icons.phone_iphone_rounded,
                      'Devices', '$deviceCount Perangkat'),
                ],
              ),
              SizedBox(height: 48.h),

              // Tombol Logout
              ListTile(
                leading: const Icon(Icons.logout, color: Colors.redAccent),
                title: const Text('Logout',
                    style: TextStyle(color: Colors.redAccent)),
                onTap: () => _handleLogout(context),
                shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(8.r),
                    side: BorderSide(
                        color: theme.colors.tertiary.withOpacity(0.1))),
              ),
            ],
          ),
        ),
      ),
    );
  }

  Widget _buildSummaryCard(
      BatThemeData theme, IconData icon, String title, String subtitle) {
    return Expanded(
      child: Container(
        padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 12.h),
        decoration: BoxDecoration(
          color: BatPalette.primary,
          borderRadius: BorderRadius.circular(16.r),
        ),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            CircleAvatar(
              radius: 24.r,
              backgroundColor: BatPalette.secondary.withOpacity(0.6),
              child: Icon(icon, color: BatPalette.white),
            ),
            SizedBox(width: 12.w),
            Expanded(
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  Text(
                    title,
                    style: theme.typography.bodyCopy.copyWith(
                        color: BatPalette.white, fontWeight: FontWeight.w500),
                  ),
                  Text(
                    subtitle,
                    style: theme.typography.subtitle
                        .copyWith(color: BatPalette.white60),
                    overflow: TextOverflow.ellipsis,
                  ),
                ],
              ),
            )
          ],
        ),
      ),
    );
  }
}
