package com.example.gallery;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.gallery.api.ApiClient;
import com.example.gallery.api.ApiService;
import com.example.gallery.models.RegisterRequest;
import com.example.gallery.models.RegisterResponse;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RegisterActivity extends AppCompatActivity {

    EditText username, email, ime, prezime, index, password;
    Button btnRegister;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        username = findViewById(R.id.usernameEditText);
        email = findViewById(R.id.emailEditText);
        ime = findViewById(R.id.imeEditText);
        prezime = findViewById(R.id.prezimeEditText);
        index = findViewById(R.id.indexEditText);
        password = findViewById(R.id.passwordEditText);
        btnRegister = findViewById(R.id.btnRegister);

        Toast.makeText(this, "RegisterActivity loaded", Toast.LENGTH_SHORT).show();

        btnRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String u = username.getText().toString().trim();
                String e = email.getText().toString().trim();
                String f = ime.getText().toString().trim();
                String l = prezime.getText().toString().trim();
                String s = index.getText().toString().trim();
                String p = password.getText().toString().trim();

                // ---------------------------
                // ✅ FRONTEND VALIDATIONS
                // ---------------------------
                if (u.isEmpty() || u.length() > 150) {
                    username.setError("Username must be 1-150 characters");
                    return;
                }

                if (e.isEmpty() || !android.util.Patterns.EMAIL_ADDRESS.matcher(e).matches()) {
                    email.setError("Enter a valid email");
                    return;
                }

                if (f.isEmpty() || f.length() > 30) {
                    ime.setError("First name must be 1-30 characters");
                    return;
                }

                if (l.isEmpty() || l.length() > 30) {
                    prezime.setError("Last name must be 1-30 characters");
                    return;
                }

                if (s.isEmpty() || s.length() > 8) {
                    index.setError("School index must be 1-8 characters");
                    return;
                }

                if (p.isEmpty() || p.length() < 8) {
                    password.setError("Password must be at least 8 characters");
                    return;
                }

                // ---------------------------
                // 🔹 API CALL
                // ---------------------------
                ApiService apiService = ApiClient.getClient().create(ApiService.class);
                RegisterRequest request = new RegisterRequest(u, e, f, l, s, p);

                apiService.register(request).enqueue(new Callback<RegisterResponse>() {
                    @Override
                    public void onResponse(Call<RegisterResponse> call, Response<RegisterResponse> response) {

                        Toast.makeText(RegisterActivity.this,
                                "HTTP code: " + response.code(),
                                Toast.LENGTH_SHORT).show();

                        if (response.isSuccessful() && response.body() != null) {
                            Toast.makeText(RegisterActivity.this,
                                    response.body().getMessage(),
                                    Toast.LENGTH_SHORT).show();
                            // Go to login
                            Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                            startActivity(intent); // go back to previous activity
                        } else {
                            try {
                                String errorBody = response.errorBody() != null
                                        ? response.errorBody().string()
                                        : "No error body";

                                Toast.makeText(RegisterActivity.this,
                                        errorBody,
                                        Toast.LENGTH_LONG).show();

                            } catch (Exception e) {
                                Toast.makeText(RegisterActivity.this,
                                        "Error reading errorBody",
                                        Toast.LENGTH_SHORT).show();
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<RegisterResponse> call, Throwable t) {
                        Toast.makeText(RegisterActivity.this,
                                "Server error: " + t.getMessage(),
                                Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });
    }
}
