import 'package:flutter/material.dart';

import '../models/school.dart';
import '../services/vanguard_api.dart';
import '../config/api_config.dart';

import '../screens/select_school_page.dart';
import '../screens/login_page.dart';
import '../screens/medical_certificates_page.dart';

class AppDrawer extends StatelessWidget {
  final String token;
  final School school;
  final String? userFirstName;

  const AppDrawer({
    super.key,
    required this.token,
    required this.school,
    this.userFirstName,
  });

  String get _name => (userFirstName ?? '').trim();
  String get _avatarLetter => _name.isNotEmpty ? _name[0].toUpperCase() : 'U';

  String? get _schoolLogoUrl {
    final raw = (school.logo ?? '').toString().trim();
    if (raw.isEmpty) return null;

    // base: https://app.yogibook.com/public/userarea/
    return '${ApiConfig.scheme}://${ApiConfig.host}/public/userarea/$raw';
  }

  Future<void> _logout(BuildContext context) async {
    final ok = await showDialog<bool>(
      context: context,
      builder: (_) => AlertDialog(
        title: const Text('Conferma logout'),
        content: const Text('Vuoi uscire dal tuo account?'),
        actions: [
          TextButton(
            onPressed: () => Navigator.pop(context, false),
            child: const Text('Annulla'),
          ),
          ElevatedButton(
            onPressed: () => Navigator.pop(context, true),
            child: const Text('Logout'),
          ),
        ],
      ),
    );

    if (ok != true) return;

    Navigator.of(context).pop(); // chiude drawer

    try {
      await VanguardApi.logout(token: token);
    } catch (_) {}

    if (!context.mounted) return;

    Navigator.of(context).pushAndRemoveUntil(
      MaterialPageRoute(builder: (_) => const LoginPage()),
      (route) => false,
    );
  }

  @override
  Widget build(BuildContext context) {
    final logoUrl = _schoolLogoUrl;

    return Drawer(
      child: SafeArea(
        child: ListView(
          padding: EdgeInsets.zero,
          children: [
            _DrawerHeaderWide(
              schoolName: school.name,
              logoUrl: logoUrl,
              fallbackLetter: _avatarLetter,
            ),

            ListTile(
              leading: const Icon(Icons.swap_horiz),
              title: const Text('Cambia scuola'),
              onTap: () {
                Navigator.of(context).pop();
                Navigator.pushReplacement(
                  context,
                  MaterialPageRoute(
                    builder: (_) => SelectSchoolPage(token: token),
                  ),
                );
              },
            ),

            // ✅ solo nel drawer
            ListTile(
              leading: const Icon(Icons.medical_information),
              title: const Text('Certificati medici'),
              onTap: () {
                Navigator.of(context).pop();
                Navigator.push(
                  context,
                  MaterialPageRoute(
                    builder: (_) => MedicalCertificatesPage(
                      token: token,
                      school: school,
                      userFirstName: userFirstName,
                    ),
                  ),
                );
              },
            ),

            const Divider(height: 1),

            ListTile(
              leading: const Icon(Icons.logout),
              title: const Text('Logout'),
              onTap: () => _logout(context),
            ),
          ],
        ),
      ),
    );
  }
}

class _DrawerHeaderWide extends StatelessWidget {
  final String schoolName;
  final String? logoUrl;
  final String fallbackLetter;

  const _DrawerHeaderWide({
    required this.schoolName,
    required this.logoUrl,
    required this.fallbackLetter,
  });

  @override
  Widget build(BuildContext context) {
    const green = Color(0xFF10B981);

    return Container(
      // Header largo quanto il drawer
      padding: const EdgeInsets.fromLTRB(16, 14, 16, 14),
      decoration: const BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
          colors: [Color(0xFFF4F6FA), Color(0xFFEFF7F3)],
        ),
      ),
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.start,
        children: [
          const Text(
            'Menu',
            style: TextStyle(fontSize: 22, fontWeight: FontWeight.w900),
          ),
          const SizedBox(height: 12),

          // ✅ BOX LOGO LARGO + PIÙ ALTO + IN RATIO (non taglia)
          Container(
            width: double.infinity,
            height: 140, // <-- aumenta qui (es. 140/160/180)
            decoration: BoxDecoration(
              color: Colors.white,
              borderRadius: BorderRadius.circular(16),
              boxShadow: const [
                BoxShadow(
                  blurRadius: 18,
                  color: Color(0x12000000),
                  offset: Offset(0, 10),
                ),
              ],
              border: Border.all(color: const Color(0x14000000)),
            ),
            child: ClipRRect(
              borderRadius: BorderRadius.circular(16),
              child: logoUrl == null
                  ? _LogoFallback(letter: fallbackLetter)
                  : Padding(
                      padding: const EdgeInsets.all(10), // respiro per logo
                      child: Image.network(
                        logoUrl!,
                        width: double.infinity, // ✅ solo larghezza “imposta”
                        fit: BoxFit.contain, // ✅ mantiene ratio, non taglia
                        errorBuilder: (_, __, ___) =>
                            _LogoFallback(letter: fallbackLetter),
                      ),
                    ),
            ),
          ),

          const SizedBox(height: 10),

          // Nome scuola sotto
          Text(
            schoolName,
            maxLines: 2,
            overflow: TextOverflow.ellipsis,
            style: const TextStyle(
              color: Colors.black87,
              fontWeight: FontWeight.w900,
              fontSize: 22,
            ),
          ),

          const SizedBox(height: 6),

          // badge piccolo opzionale, fa “design”
          Container(
            padding: const EdgeInsets.symmetric(horizontal: 10, vertical: 5),
            decoration: BoxDecoration(
              color: green.withOpacity(0.12),
              borderRadius: BorderRadius.circular(999),
            ),
            child: const Text(
              'Scuola selezionata',
              style: TextStyle(
                color: green,
                fontWeight: FontWeight.w900,
                fontSize: 11,
              ),
            ),
          ),
        ],
      ),
    );
  }
}

class _LogoFallback extends StatelessWidget {
  final String letter;
  const _LogoFallback({required this.letter});

  @override
  Widget build(BuildContext context) {
    const green = Color(0xFF10B981);

    return Container(
      width: double.infinity,
      height: double.infinity,
      alignment: Alignment.center,
      decoration: const BoxDecoration(
        gradient: LinearGradient(
          begin: Alignment.topLeft,
          end: Alignment.bottomRight,
          colors: [Color(0xFFE7F8F1), Color(0xFFF4F6FA)],
        ),
      ),
      child: Container(
        width: 44,
        height: 44,
        decoration: const BoxDecoration(color: green, shape: BoxShape.circle),
        alignment: Alignment.center,
        child: Text(
          letter,
          style: const TextStyle(
            color: Colors.white,
            fontWeight: FontWeight.w900,
            fontSize: 16,
          ),
        ),
      ),
    );
  }
}
