import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.sql.*;

public class JobPortalApp extends Application {

    Stage window;

    // 🔹 DATABASE CONNECTION
    public Connection connectDB() {
        try {
            return DriverManager.getConnection(
                    "jdbc:mysql://localhost:3306/job_system",
                    "root",
                    ""
            );
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    @Override
    public void start(Stage stage) {
        window = stage;
        showLogin();
    }

    // 🔷 LOGIN SCREEN
    public void showLogin() {

        TextField emailField = new TextField();
        emailField.setPromptText("Email");

        PasswordField passwordField = new PasswordField();
        passwordField.setPromptText("Password");

        Button loginBtn = new Button("Login");
        Button registerBtn = new Button("Register");

        Label message = new Label();

        // 🔹 LOGIN ACTION
        loginBtn.setOnAction(e -> {
            try {
                Connection conn = connectDB();

                String sql = "SELECT * FROM employers WHERE email=? AND password=?";
                PreparedStatement pst = conn.prepareStatement(sql);

                pst.setString(1, emailField.getText());
                pst.setString(2, passwordField.getText());

                ResultSet rs = pst.executeQuery();

                if (rs.next()) {
                    message.setText("Login Successful!");
                    showDashboard(); // 👉 Go to dashboard
                } else {
                    message.setText("Invalid credentials!");
                }

            } catch (Exception ex) {
                ex.printStackTrace();
            }
        });

        // 🔹 GO TO REGISTER
        registerBtn.setOnAction(e -> showRegister());

        VBox layout = new VBox(10);
        layout.setPadding(new Insets(20));

        layout.getChildren().addAll(
                new Label("LOGIN"),
                emailField,
                passwordField,
                loginBtn,
                registerBtn,
                message
        );

        window.setScene(new Scene(layout, 300, 250));
        window.setTitle("Login");
        window.show();
    }

    // 🔷 REGISTER SCREEN
    public void showRegister() {

        TextField name = new TextField();
        name.setPromptText("Name");

        TextField email = new TextField();
        email.setPromptText("Email");

        TextField phone = new TextField();
        phone.setPromptText("Phone");

        PasswordField password = new PasswordField();
        password.setPromptText("Password");

        Button registerBtn = new Button("Submit");
        Button backBtn = new Button("Back");

        Label message = new Label();

        registerBtn.setOnAction(e -> {
            try {
                Connection conn = connectDB();

                String sql = "INSERT INTO employers(name,email,phone,password) VALUES(?,?,?,?)";
                PreparedStatement pst = conn.prepareStatement(sql);

                pst.setString(1, name.getText());
                pst.setString(2, email.getText());
                pst.setString(3, phone.getText());
                pst.setString(4, password.getText());

                pst.executeUpdate();

                message.setText("Registered successfully!");

            } catch (Exception ex) {
                ex.printStackTrace();
                message.setText("Error!");
            }
        });

        backBtn.setOnAction(e -> showLogin());

        VBox layout = new VBox(10);
        layout.setPadding(new Insets(20));

        layout.getChildren().addAll(
                new Label("REGISTER"),
                name,
                email,
                phone,
                password,
                registerBtn,
                backBtn,
                message
        );

        window.setScene(new Scene(layout, 300, 300));
    }

    // 🔷 DASHBOARD (AFTER LOGIN)
    public void showDashboard() {

        Label title = new Label("Welcome to Job Dashboard");

        TextField jobTitle = new TextField();
        jobTitle.setPromptText("Job Title");

        TextField jobType = new TextField();
        jobType.setPromptText("Job Type");

        TextField salary = new TextField();
        salary.setPromptText("Salary");

        Button saveBtn = new Button("Save Job");
        Button logoutBtn = new Button("Logout");

        Label message = new Label();

        // 🔹 SAVE JOB
        saveBtn.setOnAction(e -> {
            try {
                Connection conn = connectDB();

                String sql = "INSERT INTO jobs(job_title,job_type,salary) VALUES(?,?,?)";
                PreparedStatement pst = conn.prepareStatement(sql);

                pst.setString(1, jobTitle.getText());
                pst.setString(2, jobType.getText());
                pst.setDouble(3, Double.parseDouble(salary.getText()));

                pst.executeUpdate();

                message.setText("Job Saved!");

                jobTitle.clear();
                jobType.clear();
                salary.clear();

            } catch (Exception ex) {
                ex.printStackTrace();
                message.setText("Error!");
            }
        });

        // 🔹 LOGOUT
        logoutBtn.setOnAction(e -> showLogin());

        VBox layout = new VBox(10);
        layout.setPadding(new Insets(20));

        layout.getChildren().addAll(
                title,
                jobTitle,
                jobType,
                salary,
                saveBtn,
                logoutBtn,
                message
        );

        window.setScene(new Scene(layout, 400, 300));
    }

    public static void main(String[] args) {
        launch();
    }
}