import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:siro_service/constant/links.dart';
import 'package:siro_service/controller/functions/crud.dart';
import 'package:siro_service/controller/functions/device_helper.dart';
import 'package:siro_service/controller/functions/encrypt_decrypt.dart';

import '../constant/box_name.dart';
import '../main.dart';
import '../print.dart';
import '../views/home/main.dart';

import 'package:flutter_secure_storage/flutter_secure_storage.dart';

class LoginController extends GetxController {
  var email = TextEditingController(); // Kept for UI compatibility
  var password = TextEditingController();
  final formKey = GlobalKey<FormState>();

  final FlutterSecureStorage storage = const FlutterSecureStorage();

  void login() async {
    // Ensure fingerprint is ready
    String fingerprint = box.read(BoxName.fingerPrint) ?? '';
    if (fingerprint.isEmpty) {
      fingerprint = await DeviceHelper.getDeviceFingerprint();
    }

    String? storedPassword = await storage.read(key: 'password');
    String pass = storedPassword ?? password.text;

    if (pass.isEmpty && !formKey.currentState!.validate()) return;

    var payload = {
      "fingerprint": fingerprint,
      "password": pass,
      "email": email.text.trim(),
      "aud": "service",
    };

    Log.print('🚀 Login Payload: $payload');
    var res = await CRUD().post(link: AppLink.login, payload: payload);
    Log.print('📥 Login Response: $res');

    if (res != 'failure' && res is Map && res['status'] == 'success') {
      var d = res['message']; // V1 returns {status, message: {jwt, data: {user...}}}

      // Request OTP from unified module
      String phone = d['data']['phone'] ?? '';
      bool otpSent = await _sendOtp(phone);
      if (otpSent) {
        _showOtpDialog(phone, pass, fingerprint, d);
      } else {
        Get.snackbar('Error', 'Failed to send OTP'.tr);
      }
    } else {
      Get.snackbar(
        'خطأ'.tr,
        res is Map ? res['message'].toString().tr : 'فشل تسجيل الدخول'.tr,
        backgroundColor: Colors.red.withOpacity(0.7),
        colorText: Colors.white,
      );
    }
  }

  Future<bool> _sendOtp(String phone) async {
    try {
      final response = await CRUD().post(
        link: '${AppLink.server}/auth/otp/request.php',
        payload: {
          'receiver': phone,
          'user_type': 'service'
        },
      );
      return response != 'failure';
    } catch (e) {
      Log.print('OTP SEND ERROR: $e');
      return false;
    }
  }

  void _showOtpDialog(String phone, String pass, String fingerprint, dynamic loginData) {
    String otpCode = '';
    Get.defaultDialog(
      title: 'رمز التحقق'.tr,
      content: Column(
        children: [
          Text('تم إرسال رمز التحقق إلى رقمك ($phone)'.tr),
          const SizedBox(height: 16),
          TextField(
            onChanged: (val) => otpCode = val,
            keyboardType: TextInputType.number,
            decoration: InputDecoration(
              hintText: 'أدخل الرمز هنا'.tr,
              border: OutlineInputBorder(),
            ),
          ),
        ],
      ),
      textConfirm: 'تحقق'.tr,
      confirmTextColor: Colors.white,
      onConfirm: () async {
        if (otpCode.length >= 3) {
          Get.back(); // close dialog
          await _verifyOtpAndFinalize(phone, otpCode, pass, loginData);
        } else {
          Get.snackbar('خطأ', 'الرجاء إدخال رمز صحيح'.tr);
        }
      },
    );
  }

  Future<void> _verifyOtpAndFinalize(String phone, String otp, String pass, dynamic loginData) async {
    Get.dialog(const Center(child: CircularProgressIndicator()), barrierDismissible: false);
    try {
      final response = await CRUD().post(
        link: '${AppLink.server}/auth/otp/verify.php',
        payload: {
          'phone_number': phone,
          'token_code': otp,
          'user_type': 'service',
        },
      );
      Get.back(); // close loading

      if (response != 'failure' && response['status'] == 'success') {
        // Finalize login
        final jwt = loginData['jwt'];
        final hmac = loginData['hmac'];
        await box.write(BoxName.jwt, c(jwt));
        if (hmac != null) {
          await box.write(BoxName.hmac, hmac);
        }

        var userData = loginData['data'];
        await storage.write(key: 'name', value: userData['first_name']);
        await storage.write(key: 'driverID', value: userData['id'].toString());
        await storage.write(key: 'password', value: pass);
        await box.write(BoxName.employeename, userData['first_name']);
        await box.write(BoxName.password, pass);

        Get.offAll(() => Main());
      } else {
        Get.snackbar('Error', 'Invalid OTP'.tr);
      }
    } catch (e) {
      Get.back();
      Log.print('OTP VERIFY ERROR: $e');
      Get.snackbar('Error', e.toString());
    }
  }

  @override
  void onInit() async {
    await EncryptionHelper.initialize();
    await DeviceHelper.getDeviceFingerprint();

    // Auto login if credentials exist
    String? storedPassword = await storage.read(key: 'password');
    if (storedPassword != null) {
      login();
    }

    super.onInit();
  }
}
