jQuery(document).ready(function($) {
    $('#fetch-movie').on('click', function() {
        var imdbID = $('#imdb-id').val().trim(); // e.g. tt1375666

        if (!imdbID) {
            alert('Enter IMDb ID');
            return;
        }

        $.ajax({
            url: movieAjax.ajax_url,
            type: 'POST',
            data: {
                action: 'get_movie',
                imdb_id: imdbID,
                nonce: movieAjax.nonce
            },
            success: function(response) {
                if (response.success) {
                    var movie = response.data;
                    $('#result').html(`
                        <h2>${movie.Title} (${movie.Year})</h2>
                        <img src="${movie.Poster}" alt="${movie.Title}" style="max-width:300px;">
                        <p><strong>Plot:</strong> ${movie.Plot}</p>
                        <p><strong>IMDb Rating:</strong> ${movie.imdbRating}/10</p>
                        <p><strong>Director:</strong> ${movie.Director}</p>
                    `);
                } else {
                    alert('Error: ' + response.data.message);
                }
            },
            error: function() {
                alert('AJAX error');
            }
        });
    });
});