import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:share_plus/share_plus.dart';
import 'package:url_launcher/url_launcher.dart';
import '../../../constant/finance_design_system.dart';
import '../../../controller/gamification/referral_controller.dart';
import 'package:siro_driver/views/widgets/error_snakbar.dart';

class ReferralCenterPage extends StatelessWidget {
  ReferralCenterPage({super.key});
  final ReferralController controller = Get.put(ReferralController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      backgroundColor: FinanceDesignSystem.backgroundColor,
      body: GetBuilder<ReferralController>(builder: (rc) {
        if (rc.isLoading) {
          return Center(
              child: CircularProgressIndicator(
                  color: FinanceDesignSystem.primaryDark));
        }
        return CustomScrollView(physics: const BouncingScrollPhysics(), slivers: [
          // ═══ Header ═══
          SliverAppBar(
            expandedHeight: 220, pinned: true,
            backgroundColor: const Color(0xFF0A0E21),
            leading: IconButton(icon: const Icon(Icons.arrow_back_ios_new_rounded, color: Colors.white, size: 20), onPressed: () => Get.back()),
            flexibleSpace: FlexibleSpaceBar(
              centerTitle: true,
              title: Text('Referral Center'.tr, style: const TextStyle(color: Colors.white, fontWeight: FontWeight.bold, fontSize: 18)),
              background: Stack(fit: StackFit.expand, children: [
                Container(decoration: const BoxDecoration(gradient: LinearGradient(colors: [Color(0xFF0A0E21), Color(0xFF311B92)]))),
                Positioned(right: -50, bottom: -30, child: Icon(Icons.share_rounded, size: 200, color: Colors.white.withValues(alpha: 0.03))),
                // Stats in header
                Positioned(bottom: 60, left: 24, right: 24, child: Row(children: [
                  _headerStat('${rc.totalReferrals}', 'Total Invites'.tr),
                  _headerStat('${rc.activeReferrals}', 'Active'.tr),
                  _headerStat('${rc.totalRewardsEarned.toStringAsFixed(0)}', 'Rewards'.tr),
                ])),
              ]),
            ),
          ),
          SliverPadding(padding: const EdgeInsets.fromLTRB(16, 24, 16, 40), sliver: SliverList(delegate: SliverChildListDelegate([
            // ═══ Referral Code Card ═══
            _buildCodeCard(rc),
            const SizedBox(height: 20),
            // ═══ Share Buttons ═══
            _buildShareSection(rc),
            const SizedBox(height: 24),
            // ═══ How it Works ═══
            _buildHowItWorks(),
            const SizedBox(height: 24),
            // ═══ Driver Referrals ═══
            if (rc.driverReferrals.isNotEmpty) ...[
              Text('Driver Invitations'.tr, style: FinanceDesignSystem.headingStyle),
              const SizedBox(height: 12),
              ...rc.driverReferrals.map((r) => _referralItem(r, const Color(0xFF2196F3))),
              const SizedBox(height: 20),
            ],
            // ═══ Passenger Referrals ═══
            if (rc.passengerReferrals.isNotEmpty) ...[
              Text('Passenger Invitations'.tr, style: FinanceDesignSystem.headingStyle),
              const SizedBox(height: 12),
              ...rc.passengerReferrals.map((r) => _referralItem(r, const Color(0xFF4CAF50))),
            ],
            if (rc.driverReferrals.isEmpty && rc.passengerReferrals.isEmpty)
              _buildEmptyState(),
          ]))),
        ]);
      }),
    );
  }

  Widget _headerStat(String value, String label) {
    return Expanded(child: Column(children: [
      Text(value, style: const TextStyle(fontSize: 22, fontWeight: FontWeight.w900, color: Colors.white)),
      Text(label, style: TextStyle(fontSize: 10, color: Colors.white.withValues(alpha: 0.6))),
    ]));
  }

  Widget _buildCodeCard(ReferralController rc) {
    return Container(
      padding: const EdgeInsets.all(24),
      decoration: BoxDecoration(
        gradient: const LinearGradient(colors: [Color(0xFF1A237E), Color(0xFF311B92)]),
        borderRadius: BorderRadius.circular(FinanceDesignSystem.cardRadius),
        boxShadow: [BoxShadow(color: const Color(0xFF311B92).withValues(alpha: 0.3), blurRadius: 15, offset: const Offset(0, 8))],
      ),
      child: Column(children: [
        Text('Your Referral Code'.tr, style: TextStyle(color: Colors.white.withValues(alpha: 0.7), fontSize: 14)),
        const SizedBox(height: 12),
        Container(
          padding: const EdgeInsets.symmetric(horizontal: 24, vertical: 12),
          decoration: BoxDecoration(color: Colors.white.withValues(alpha: 0.15), borderRadius: BorderRadius.circular(12), border: Border.all(color: Colors.white.withValues(alpha: 0.2))),
          child: Row(mainAxisSize: MainAxisSize.min, children: [
            Text(rc.referralCode, style: const TextStyle(fontSize: 24, fontWeight: FontWeight.w900, color: Colors.white, letterSpacing: 3)),
            const SizedBox(width: 12),
            GestureDetector(
              onTap: () { rc.copyCode(); mySnackbarSuccess('Code copied!'.tr); },
              child: Container(
                padding: const EdgeInsets.all(6),
                decoration: BoxDecoration(color: Colors.white.withValues(alpha: 0.2), borderRadius: BorderRadius.circular(8)),
                child: const Icon(Icons.copy_rounded, color: Colors.white, size: 18),
              ),
            ),
          ]),
        ),
        const SizedBox(height: 12),
        Text('Share this code to earn rewards'.tr, style: TextStyle(color: Colors.white.withValues(alpha: 0.5), fontSize: 12)),
      ]),
    );
  }

  Widget _buildShareSection(ReferralController rc) {
    return Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
      Text('Share via'.tr, style: FinanceDesignSystem.headingStyle),
      const SizedBox(height: 12),
      Row(children: [
        _shareButton(Icons.share_rounded, 'Share'.tr, FinanceDesignSystem.accentBlue, () => Share.share(rc.shareMessage)),
        const SizedBox(width: 12),
        _shareButton(Icons.chat_rounded, 'WhatsApp'.tr, const Color(0xFF25D366), () => _shareWhatsApp(rc.shareMessage)),
        const SizedBox(width: 12),
        _shareButton(Icons.person_add_rounded, 'Invite Driver'.tr, const Color(0xFFFF9800), () => _showInviteDialog(rc, 'driver')),
        const SizedBox(width: 12),
        _shareButton(Icons.hail_rounded, 'Invite Rider'.tr, const Color(0xFF9C27B0), () => _showInviteDialog(rc, 'passenger')),
      ]),
    ]);
  }

  Widget _shareButton(IconData icon, String label, Color color, VoidCallback onTap) {
    return Expanded(child: GestureDetector(
      onTap: onTap,
      child: Container(
        padding: const EdgeInsets.symmetric(vertical: 14),
        decoration: BoxDecoration(color: color.withValues(alpha: 0.1), borderRadius: BorderRadius.circular(14)),
        child: Column(children: [
          Icon(icon, color: color, size: 24),
          const SizedBox(height: 6),
          Text(label, style: TextStyle(fontSize: 9, fontWeight: FontWeight.w600, color: color), textAlign: TextAlign.center, maxLines: 1, overflow: TextOverflow.ellipsis),
        ]),
      ),
    ));
  }

  Widget _buildHowItWorks() {
    return Container(
      padding: const EdgeInsets.all(20),
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(FinanceDesignSystem.cardRadius),
        boxShadow: [BoxShadow(color: Colors.black.withValues(alpha: 0.03), blurRadius: 10, offset: const Offset(0, 4))]),
      child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
        Text('How It Works'.tr, style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold, color: FinanceDesignSystem.primaryDark)),
        const SizedBox(height: 16),
        _step('1', 'Share your code'.tr, 'Send your referral code to friends'.tr, const Color(0xFF2196F3)),
        _step('2', 'Friend signs up'.tr, 'They register using your code'.tr, const Color(0xFFFF9800)),
        _step('3', 'Both earn rewards'.tr, 'You get 100 pts, they get 50 pts'.tr, const Color(0xFF4CAF50)),
        _step('4', 'Bonus at 10 trips'.tr, 'Extra 200 pts when they complete 10 trips'.tr, const Color(0xFF9C27B0)),
      ]),
    );
  }

  Widget _step(String num, String title, String desc, Color color) {
    return Padding(padding: const EdgeInsets.only(bottom: 12), child: Row(children: [
      Container(width: 32, height: 32, decoration: BoxDecoration(color: color.withValues(alpha: 0.15), borderRadius: BorderRadius.circular(8)),
        child: Center(child: Text(num, style: TextStyle(fontSize: 14, fontWeight: FontWeight.bold, color: color))),
      ),
      const SizedBox(width: 12),
      Expanded(child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
        Text(title, style: TextStyle(fontSize: 13, fontWeight: FontWeight.w600, color: FinanceDesignSystem.primaryDark)),
        Text(desc, style: TextStyle(fontSize: 11, color: Colors.grey.shade500)),
      ])),
    ]));
  }

  Widget _referralItem(ReferralRecord r, Color color) {
    return Container(
      margin: const EdgeInsets.only(bottom: 10),
      padding: const EdgeInsets.all(14),
      decoration: BoxDecoration(color: Colors.white, borderRadius: BorderRadius.circular(14),
        boxShadow: [BoxShadow(color: Colors.black.withValues(alpha: 0.02), blurRadius: 8, offset: const Offset(0, 3))]),
      child: Row(children: [
        Container(width: 40, height: 40, decoration: BoxDecoration(color: color.withValues(alpha: 0.1), borderRadius: BorderRadius.circular(10)),
          child: Icon(r.type == 'driver' ? Icons.local_taxi_rounded : Icons.person_rounded, color: color, size: 20)),
        const SizedBox(width: 12),
        Expanded(child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: [
          Text(r.name.isNotEmpty ? r.name : r.phone, style: const TextStyle(fontSize: 13, fontWeight: FontWeight.w600)),
          Text(r.joinDate, style: TextStyle(fontSize: 10, color: Colors.grey.shade500)),
        ])),
        Container(
          padding: const EdgeInsets.symmetric(horizontal: 8, vertical: 4),
          decoration: BoxDecoration(
            color: r.status == 'active' ? FinanceDesignSystem.successGreen.withValues(alpha: 0.1)
                : r.status == 'registered' ? FinanceDesignSystem.accentBlue.withValues(alpha: 0.1) : Colors.grey.shade100,
            borderRadius: BorderRadius.circular(8)),
          child: Text(r.status.tr, style: TextStyle(fontSize: 10, fontWeight: FontWeight.w600,
            color: r.status == 'active' ? FinanceDesignSystem.successGreen
                : r.status == 'registered' ? FinanceDesignSystem.accentBlue : Colors.grey)),
        ),
      ]),
    );
  }

  Widget _buildEmptyState() {
    return Center(child: Padding(padding: const EdgeInsets.symmetric(vertical: 40), child: Column(children: [
      Icon(Icons.people_outline_rounded, size: 60, color: Colors.grey.shade300),
      const SizedBox(height: 12),
      Text('No invitation found yet!'.tr, style: TextStyle(fontSize: 14, color: Colors.grey.shade400)),
      const SizedBox(height: 4),
      Text('Start sharing your code!'.tr, style: TextStyle(fontSize: 12, color: Colors.grey.shade300)),
    ])));
  }

  void _shareWhatsApp(String message) async {
    final url = 'https://wa.me/?text=${Uri.encodeComponent(message)}';
    if (await canLaunchUrl(Uri.parse(url))) {
      await launchUrl(Uri.parse(url), mode: LaunchMode.externalApplication);
    }
  }

  void _showInviteDialog(ReferralController rc, String type) {
    final phoneController = TextEditingController();
    Get.defaultDialog(
      title: type == 'driver' ? 'Invite Driver'.tr : 'Invite Rider'.tr,
      titleStyle: FinanceDesignSystem.headingStyle,
      content: Column(children: [
        Text('Enter phone number'.tr, style: FinanceDesignSystem.subHeadingStyle),
        const SizedBox(height: 12),
        TextField(
          controller: phoneController, keyboardType: TextInputType.phone,
          decoration: InputDecoration(hintText: '09XX XXX XXX', prefixIcon: const Icon(Icons.phone_rounded),
            border: OutlineInputBorder(borderRadius: BorderRadius.circular(12))),
        ),
      ]),
      confirm: ElevatedButton(
        onPressed: () async {
          if (phoneController.text.length >= 10) {
            Get.back();
            bool success = type == 'driver'
                ? await rc.inviteDriver(phoneController.text)
                : await rc.invitePassenger(phoneController.text);
            if (success) mySnackbarSuccess('Invite sent successfully'.tr);
          }
        },
        style: ElevatedButton.styleFrom(backgroundColor: FinanceDesignSystem.accentBlue, foregroundColor: Colors.white,
          shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(12)), padding: const EdgeInsets.symmetric(horizontal: 32, vertical: 12)),
        child: Text('Send Invite'.tr),
      ),
      cancel: TextButton(onPressed: () => Get.back(), child: Text('Cancel'.tr, style: const TextStyle(color: Colors.grey))),
    );
  }
}
