package com.example.pap_teste;

import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

/**
 * Tela de confirmação apresentada logo após a criação de conta.
 * Exibe os dados básicos e direciona para a área correspondente
 * (estabelecimento como admin, cliente como cliente).
 */
public class AccountCreatedActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_account_created);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.accountCreatedRoot), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });

        TextView txtWelcome = findViewById(R.id.txtWelcome);
        TextView txtEmail = findViewById(R.id.txtEmail);
        TextView txtRole = findViewById(R.id.txtRole);
        Button btnContinue = findViewById(R.id.btnContinuar);
        Button btnBack = findViewById(R.id.btnVoltar);

        String displayName = getIntent().getStringExtra(MainActivity.EXTRA_DISPLAY_NAME);
        String email = getIntent().getStringExtra(MainActivity.EXTRA_EMAIL);
        String accountType = getIntent().getStringExtra(MainActivity.EXTRA_ACCOUNT_TYPE);
        String role = getIntent().getStringExtra(MainActivity.EXTRA_ROLE);

        txtWelcome.setText(displayName != null ? displayName : "Novo utilizador");
        txtEmail.setText(email != null ? email : "Email não informado");
        txtRole.setText(role != null ? role : "CLIENTE");

        btnContinue.setOnClickListener(v -> {
            boolean isEstablishment = "ESTABELECIMENTO".equalsIgnoreCase(accountType);
            Intent nextScreen = new Intent(
                    this,
                    isEstablishment ? EstablishmentDashboardActivity.class : ClientDashboardActivity.class
            );
            nextScreen.putExtra(MainActivity.EXTRA_ACTION_MODE, MainActivity.AccountAction.CRIAR.name());
            nextScreen.putExtra(MainActivity.EXTRA_DISPLAY_NAME, displayName);
            nextScreen.putExtra(MainActivity.EXTRA_EMAIL, email);
            nextScreen.putExtra(MainActivity.EXTRA_ACCOUNT_TYPE, accountType);
            nextScreen.putExtra(MainActivity.EXTRA_ROLE, role);
            startActivity(nextScreen);
            finish();
        });

        if (btnBack != null) {
            btnBack.setOnClickListener(v -> finish());
        }
    }
}

