
import 'package:bookmywages/consts_widgets/app_assets.dart';
import 'package:bookmywages/consts_widgets/app_colors.dart';
import 'package:bookmywages/routers/consts_router.dart';
import 'package:flutter/material.dart';

import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:get/get.dart';
import 'package:shared_preferences/shared_preferences.dart';

class VendorWelcomePage extends ConsumerStatefulWidget {
  const VendorWelcomePage({super.key});

  @override
  ConsumerState<VendorWelcomePage> createState() => _VendorWelcomePageState();
}

class _VendorWelcomePageState extends ConsumerState<VendorWelcomePage> {
  bool _isLoading = true;

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

  Future<void> _checkProfileStatus() async {
    try {
      setState(() => _isLoading = true);

      final prefs = await SharedPreferences.getInstance();
      final vendorId = prefs.getString('vendor_id');
      final vendorStatus = prefs.getString('vendor_status');

      print('Stored vendor_id: $vendorId');
      print('Stored vendor_status: $vendorStatus');

      await Future.delayed(const Duration(seconds: 2));

      if (!mounted) return;

      if (vendorId == null || vendorId.isEmpty) {
        // Case 1: vendor_id is null or empty
        Get.offNamed(RouterConts.vendorresgister);
      } else if (vendorStatus == '0') {
        // Case 2: vendor_id present but vendor_status is null
        _showVerificationDialog(context);
      } else {
        // Case 3: both are present
       Get.offNamed(RouterConts.vendorhomepage);
      }
    } catch (e) {
      // ignore: avoid_print
      print('Error checking profile status: $e');
      if (mounted) {
        _showVerificationDialog(context);
      }
    } finally {
      if (mounted) {
        setState(() => _isLoading = false);
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    return SafeArea(
      child: Scaffold(
        backgroundColor: AppColors.secondprimary,
        body: LayoutBuilder(
          builder: (context, constraints) {
            return Column(
              children: [
                Padding(
                  padding: const EdgeInsets.only(
                    left: 33.0,
                    top: 48,
                    right: 59,
                  ),
                  child: Align(
                    alignment: Alignment.topLeft,
                    child: Text.rich(
                      TextSpan(
                        children: [
                          TextSpan(
                            text: "Welcome back to ",
                            style: TextStyle(
                              fontFamily: 'Gilroy-Medium',
                              fontWeight: FontWeight.w400,
                              fontSize: 32,
                              height: 1.5,
                              letterSpacing: 0.26,
                            ),
                          ),
                          TextSpan(
                            text: "VENDOR LOGIN",
                            style: TextStyle(
                              fontFamily: 'Gilroy-ExtraBold',
                              fontWeight: FontWeight.w800,
                              fontSize: 32,
                              height: 1.5,
                              letterSpacing: 0.26,
                              color: Color(0xFF3A47C9),
                            ),
                          ),
                        ],
                      ),
                    ),
                  ),
                ),
                Expanded(
                  child: Center(
                    child: Image.asset(
                      AppAssets.vendorwelcome,
                      fit: BoxFit.contain,
                      width: constraints.maxWidth * 0.8,
                    ),
                  ),
                ),
              ],
            );
          },
        ),
      ),
    );
  }

  void _showVerificationDialog(BuildContext context) {
    showDialog(
      context: context,
      barrierDismissible: false, // Prevent closing by tapping outside
      builder: (BuildContext context) {
        return Dialog(
          shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(30),
            side: const BorderSide(color: Colors.white, width: 1),
          ),
          elevation: 10,
          backgroundColor: Colors.white,
          child: Container(
            width: 365,
            height: 305,
            padding: const EdgeInsets.all(20),
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: [
                const Text(
                  'Verification',
                  style: TextStyle(
                    fontFamily: 'Gilroy-Bold',
                    fontWeight: FontWeight.w700,
                    fontSize: 32,
                    height: 1.0,
                    letterSpacing: 0.32,
                  ),
                ),
                const SizedBox(height: 20),
                const Text(
                  'Still your account will not verify by Admin, please wait with us.',
                  textAlign: TextAlign.center,
                  style: TextStyle(
                    fontFamily: 'Gilroy-Bold',
                    fontWeight: FontWeight.w700,
                    fontSize: 20.23,
                    height: 1.74,
                    letterSpacing: 0.2023,
                  ),
                ),
                const SizedBox(height: 30),
                ElevatedButton(
                  style: ElevatedButton.styleFrom(
                    backgroundColor: const Color(0xFF0066FF),
                    shape: RoundedRectangleBorder(
                      borderRadius: BorderRadius.circular(29.36),
                    ),
                    padding: const EdgeInsets.symmetric(
                      horizontal: 50,
                      vertical: 15,
                    ),
                  ),
                  onPressed: () {
                    Navigator.of(context).pop(); // Close dialog
                    Get.offNamed(RouterConts.homescreen);
                  },
                  child: const Text(
                    'OK',
                    style: TextStyle(
                      fontFamily: 'Gilroy-Black',
                      fontWeight: FontWeight.w400,
                      fontSize: 20.29,
                      height: 1.0,
                      letterSpacing: 0.4058,
                      color: Colors.white,
                    ),
                  ),
                ),
              ],
            ),
          ),
        );
      },
    );
  }
}
