import 'package:flutter/material.dart';
import 'package:login_page/screens/login_screen.dart'; // Make sure this import path is correct

class HomePage extends StatelessWidget {
  final String email;

  const HomePage({super.key, required this.email});

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("Dashboard"),
        automaticallyImplyLeading: false,
        actions: [
          // Logout Icon Button
          IconButton(
            icon: Icon(Icons.logout, color: Colors.black),
            onPressed: () {
              Navigator.of(context).pushAndRemoveUntil(
                MaterialPageRoute(builder: (context) => LoginScreen()),
                (route) => false,
              );
            },
          ),
        ],
      ),
      body: SingleChildScrollView(
        child: Padding(
          padding: EdgeInsets.all(20),
          child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              // Welcome Section Container with Greeting Inside
              Container(
                padding: EdgeInsets.all(15),
                decoration: BoxDecoration(
                  border: Border.all(color: Colors.grey.shade300),
                  borderRadius: BorderRadius.circular(8),
                ),
                child: Column(
                  crossAxisAlignment: CrossAxisAlignment.start,
                  children: [
                    // Greeting + Waving Hand Icon
                    Row(
                      children: [
                        Text(
                          "Good Morning, Thereza",
                          style: TextStyle(
                            fontSize: 18,
                            fontWeight: FontWeight.bold,
                          ),
                        ),
                        SizedBox(width: 5),
                        Icon(Icons.waving_hand, size: 24, color: Colors.blue),
                      ],
                    ),
                    SizedBox(height: 10),

                    // Prepaid Info
                    Text("Prepaid: 0794606921"),
                    SizedBox(height: 5),
                    Text("Credit (Ksh): 0"),
                    Text("Net Points: 0"),
                  ],
                ),
              ),

              SizedBox(height: 20),

              // Full Width Wholesome Image - No Background
              Container(
                margin: EdgeInsets.symmetric(vertical: 10),
                decoration: BoxDecoration(
                  borderRadius: BorderRadius.circular(10),
                ),
                child: ClipRRect(
                  borderRadius: BorderRadius.circular(10),
                  child: Image.asset(
                    'assets/gas.jpeg',
                    width: double.infinity,
                    height: 250, // Fallback height for most devices
                    fit: BoxFit.contain,
                  ),
                ),
              ),

              SizedBox(height: 20),

              // Active Subscription Title
              Text(
                "Your active subscriptions",
                style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
              ),

              SizedBox(height: 10),

              // Active Subscription Card
              Card(
                elevation: 3,
                child: Padding(
                  padding: EdgeInsets.all(15),
                  child: Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(
                        "KKWZBZVZ",
                        style: TextStyle(fontWeight: FontWeight.bold),
                      ),
                      SizedBox(height: 5),
                      Text("Sh850= 30Days UnlimiNET - 30 Days"),
                      SizedBox(height: 5),
                      Text("Used: 3.96 GB"),
                      SizedBox(height: 5),
                      Text("Expires: 14/06/2025 17:55"),
                      Align(
                        alignment: Alignment.centerRight,
                        child: ElevatedButton(
                          style: ElevatedButton.styleFrom(
                            backgroundColor: Colors.green,
                            foregroundColor: Colors.white,
                          ),
                          onPressed: () {},
                          child: Text("RECONNECT"),
                        ),
                      ),
                    ],
                  ),
                ),
              ),

              SizedBox(height: 20),

              // Enter Voucher Code
              TextField(
                decoration: InputDecoration(
                  hintText: "Enter Voucher Code",
                  suffixIcon: ElevatedButton(
                    style: ElevatedButton.styleFrom(
                      backgroundColor: Colors.blue,
                      foregroundColor: Colors.white,
                    ),
                    onPressed: () {},
                    child: Text("CONNECT"),
                  ),
                ),
              ),

              SizedBox(height: 20),

              // Offers Title
              Text(
                "Offers",
                style: TextStyle(fontSize: 16, fontWeight: FontWeight.bold),
              ),

              SizedBox(height: 10),

              // Offers List
              _buildOfferCard("Daily FREE 20 Minutes UnlimiNET", "FREE"),
              _buildOfferCard("Sh5= 30Minutes UnlimiNET", "Sh5"),
              _buildOfferCard("Sh9= 1Hour UnlimiNET", "Sh9"),
              _buildOfferCard("Sh13= 2Hours UnlimiNET", "Sh13"),
            ],
          ),
        ),
      ),
    );
  }

  // Reusable widget for each offer card
  Widget _buildOfferCard(String name, String price) {
    return Card(
      margin: EdgeInsets.symmetric(vertical: 5),
      child: Padding(
        padding: EdgeInsets.all(10),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          children: [
            Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: [
                Text(name, style: TextStyle(fontWeight: FontWeight.bold)),
                Text("1 Device"),
              ],
            ),
            price == "FREE"
                ? OutlinedButton(
                  style: OutlinedButton.styleFrom(
                    foregroundColor: Colors.red,
                    side: BorderSide(color: Colors.red),
                  ),
                  onPressed: () {},
                  child: Text("FREE"),
                )
                : OutlinedButton(
                  style: OutlinedButton.styleFrom(
                    foregroundColor: Colors.red,
                    side: BorderSide(color: Colors.red),
                  ),
                  onPressed: () {},
                  child: Text("BUY"),
                ),
          ],
        ),
      ),
    );
  }
}
