import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:medora/data/models/doctor.dart';
import 'package:medora/data/services/doctor_profile_service.dart';
import 'package:medora/route/route_names.dart';

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

  @override
  State<DoctorPersonalProfileScreen> createState() =>
      _DoctorPersonalProfileScreen();
}

class _DoctorPersonalProfileScreen extends State<DoctorPersonalProfileScreen> {
  final FirebaseAuth _auth = FirebaseAuth.instance;
  // final GlobalKey<CurvedNavigationBarState> _bottomNavigationKey = GlobalKey();
  Doctor? _doctorProfile;

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

  Future<void> _fetchDoctorProfile() async {
    final doctorProfile = await DoctorProfileService.getDoctorProfile();
    if (mounted) {
      setState(() {
        _doctorProfile = doctorProfile;
      });
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          children: [
            _buildProfileHeader(),
            _buildProfileOptions(),
            const Spacer(),
            // _buildBottomNavBar(),
          ],
        ),
      ),
    );
  }

  Widget _buildProfileHeader() {
    return Container(
      padding: const EdgeInsets.all(16),
      decoration: const BoxDecoration(
        gradient: LinearGradient(
          colors: [
            Color(0xFF00BCD4),
            Color(0xFF2196F3),
          ],
          begin: Alignment.centerLeft,
          end: Alignment.centerRight,
        ),
        borderRadius: BorderRadius.only(
          bottomLeft: Radius.circular(20),
          bottomRight: Radius.circular(20),
        ),
      ),
      child: Row(
        children: [
          Container(
            width: 60,
            height: 60,
            decoration: BoxDecoration(
              color: Colors.white,
              shape: BoxShape.circle,
              image: _doctorProfile?.profileImageUrl != null
                  ? DecorationImage(
                      image: NetworkImage(_doctorProfile!.profileImageUrl!),
                      fit: BoxFit.cover,
                    )
                  : null,
            ),
            child: const Center(
                // child: Text(
                //   _doctorProfile != null && _doctorProfile!.firstName != null
                //       ? _doctorProfile!.firstName![0].toUpperCase()
                //       : '',
                //   style: const TextStyle(
                //     fontSize: 30,
                //     fontWeight: FontWeight.bold,
                //     color: Colors.blue,
                //   ),
                // ),
                ),
          ),
          const SizedBox(width: 16),
          Expanded(
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(
                  _doctorProfile != null && _doctorProfile!.firstName != null
                      ? _doctorProfile!.firstName!
                      : 'Welcome',
                  style: const TextStyle(
                    fontSize: 24,
                    fontWeight: FontWeight.bold,
                    color: Colors.white,
                  ),
                ),
                const Text(
                  'Personal Profile',
                  style: TextStyle(
                    fontSize: 14,
                    color: Colors.white70,
                  ),
                ),
              ],
            ),
          ),
          const Icon(
            Icons.chevron_right,
            color: Colors.white,
            size: 30,
          ),
        ],
      ),
    );
  }

  Widget _buildProfileOptions() {
    return Container(
      margin: const EdgeInsets.all(16),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(16),
        boxShadow: [
          BoxShadow(
            color: Colors.grey.withOpacity(0.1),
            spreadRadius: 1,
            blurRadius: 5,
          ),
        ],
      ),
      child: Column(
        children: [
          _buildOptionTile(' Profile', Icons.medical_information_outlined,
              onTap: () {}, iconColor: Colors.blue),
          const Divider(height: 1),
          _buildOptionTile(
            'Sign Out',
            Icons.logout,
            onTap: () {
              _signOut();
            },
            iconColor: Colors.blue,
          ),
        ],
      ),
    );
  }

  Widget _buildOptionTile(String title, IconData icon,
      {required VoidCallback onTap, Color? iconColor}) {
    return ListTile(
      leading: Icon(
        icon,
        color: iconColor ?? Colors.grey,
        size: 24,
      ),
      title: Text(
        title,
        style: const TextStyle(
          fontSize: 16,
          fontWeight: FontWeight.w500,
        ),
      ),
      trailing: const Icon(
        Icons.chevron_right,
        color: Colors.grey,
      ),
      onTap: onTap,
    );
  }

  Future<void> _signOut() async {
    try {
      await _auth.signOut();
      if (mounted) {
        Navigator.of(context).pushReplacementNamed(RouteNames.signIn);
      }
    } catch (e) {
      print("Error signing out: $e");
      if (mounted) {
        ScaffoldMessenger.of(context).showSnackBar(
          const SnackBar(content: Text('Failed to log out. Please try again.')),
        );
      }
    }
  }
}
