import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:get/get.dart';
import 'package:flutter/material.dart';
import 'package:nutriplant/app/routes/app_pages.dart';

class SignupController extends GetxController {
  var nameController = TextEditingController();
  var emailController = TextEditingController();
  var passwordController = TextEditingController();
  var isLoading = false.obs;

  final FirebaseAuth _auth = FirebaseAuth.instance;
  final FirebaseFirestore _firestore = FirebaseFirestore.instance;

  void signup() async {
    String name = nameController.text.trim();
    String email = emailController.text.trim();
    String password = passwordController.text.trim();

    if (name.isEmpty || email.isEmpty || password.isEmpty) {
      showSnackbar("Error", "Semua kolom harus diisi!", Colors.redAccent);
      return;
    }

    isLoading.value = true;

    try {
      // Hapus keyboard
      Get.focusScope?.unfocus();

      // Registrasi user
      UserCredential userCredential = await _auth.createUserWithEmailAndPassword(
        email: email,
        password: password,
      );

      // Simpan data user ke Firestore
      await _firestore.collection("users").doc(userCredential.user!.uid).set({
        "name": name,
        "email": email,
        "createdAt": FieldValue.serverTimestamp(),
      });

      showSnackbar("Success", "Registrasi berhasil!", Colors.green);
      Get.offAllNamed(Routes.LOGIN);
    } on FirebaseAuthException catch (e) {
      String errorMessage = "Terjadi kesalahan.";
      if (e.code == 'email-already-in-use') {
        errorMessage = "Email sudah digunakan.";
      } else if (e.code == 'weak-password') {
        errorMessage = "Gunakan password yang lebih kuat.";
      }
      showSnackbar("Signup Gagal", errorMessage, Colors.redAccent);
    } catch (e) {
      showSnackbar("Error", "Gagal mendaftar: ${e.toString()}", Colors.redAccent);
    } finally {
      isLoading.value = false;
    }
  }

  void showSnackbar(String title, String message, Color backgroundColor) {
    Get.snackbar(
      title,
      message,
      snackPosition: SnackPosition.BOTTOM,
      backgroundColor: backgroundColor,
      colorText: Colors.white,
    );
  }

  @override
  void onClose() {
    nameController.dispose();
    emailController.dispose();
    passwordController.dispose();
    super.onClose();
  }
}
