import 'package:flutter/material.dart';
import 'package:descope/descope.dart';
import 'package:go_router/go_router.dart';
import 'package:google_fonts/google_fonts.dart';
import '../../../../core/notifiers/auth_notifier.dart';

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

  Future<void> _logout(BuildContext context) async {
    // ignore: use_build_context_synchronously
    Descope.sessionManager.clearSession();
    AuthNotifier.instance.notify();
  }

  void _onScanQR(BuildContext context) {
    context.push('/scan');
  }

  @override
  Widget build(BuildContext context) {
    final user = Descope.sessionManager.session?.user;
    final userName = user?.name ?? user?.email ?? user?.phone ?? 'User';

    return Scaffold(
      backgroundColor: Colors.grey[50],
      appBar: AppBar(
        title: Text('Baron SSO', style: GoogleFonts.outfit(fontWeight: FontWeight.bold)),
        elevation: 0,
        backgroundColor: Colors.white,
        foregroundColor: Colors.black,
        actions: [
          IconButton(
            icon: const Icon(Icons.logout),
            onPressed: () => _logout(context),
            tooltip: 'Sign Out',
          ),
        ],
      ),
      body: Center(
        child: Padding(
          padding: const EdgeInsets.all(24.0),
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: [
              const Icon(Icons.check_circle_outline, size: 80, color: Colors.green),
              const SizedBox(height: 24),
              Text(
                '로그인 성공!',
                style: GoogleFonts.notoSans(
                  fontSize: 28,
                  fontWeight: FontWeight.bold,
                  color: const Color(0xFF1A1F2C),
                ),
              ),
              const SizedBox(height: 8),
              Text(
                '반갑습니다, $userName님',
                style: GoogleFonts.notoSans(
                  fontSize: 16,
                  color: Colors.grey[600],
                ),
              ),
              const SizedBox(height: 48),
              
              // QR Camera Button
              SizedBox(
                width: double.infinity,
                height: 56,
                child: ElevatedButton(
                  onPressed: () => _onScanQR(context),
                  style: ElevatedButton.styleFrom(
                    backgroundColor: const Color(0xFF1A1F2C),
                    foregroundColor: Colors.white,
                    elevation: 2,
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(12),
                    ),
                  ),
                  child: Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      const Icon(Icons.qr_code_scanner, size: 28),
                      const SizedBox(width: 12),
                      Text(
                        'QR 스캔하기',
                        style: GoogleFonts.notoSans(fontSize: 18, fontWeight: FontWeight.w600),
                      ),
                      const SizedBox(width: 40), // Icon size(28) + Spacing(12) to balance the centering
                    ],
                  ),
                ),
              ),
              const SizedBox(height: 16),
              const Text(
                'PC 화면의 QR 코드를 스캔하여 로그인하세요.',
                style: TextStyle(color: Colors.grey, fontSize: 13),
              ),
              const SizedBox(height: 32),
              
              // My Page Button
              OutlinedButton.icon(
                onPressed: () => context.push('/profile'),
                icon: const Icon(Icons.person),
                label: const Text('내 정보 보기'),
                style: OutlinedButton.styleFrom(
                  foregroundColor: const Color(0xFF1A1F2C),
                  side: const BorderSide(color: Color(0xFF1A1F2C)),
                  padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
