<?php
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

$regions = [
    'IN' => 'India (Default)', 'US' => 'United States', 'GB' => 'United Kingdom', 'PK' => 'Pakistan', 'CA' => 'Canada',
    'AU' => 'Australia', 'DE' => 'Germany', 'FR' => 'France', 'JP' => 'Japan', 'CN' => 'China', 'BR' => 'Brazil', 'RU' => 'Russia',
    'ZA' => 'South Africa', 'NG' => 'Nigeria', 'KE' => 'Kenya', 'EG' => 'Egypt', 'AE' => 'United Arab Emirates', 'SA' => 'Saudi Arabia',
    'SG' => 'Singapore', 'MY' => 'Malaysia', 'TH' => 'Thailand', 'ID' => 'Indonesia', 'PH' => 'Philippines', 'BD' => 'Bangladesh', 'LK' => 'Sri Lanka'
];
?>
<div class="wrap trendly-wrap">
  <h1><?php echo esc_html__( 'Trendly AI Post - Settings', 'trendly-ai-post' ); ?></h1>
  <form method="post" action="options.php">
    <?php settings_fields( 'trendly_ai_post_options_group' ); ?>
    <table class="form-table" role="presentation">
      <tbody>
        <tr>
          <th scope="row"><label for="gemini_api_key"><?php esc_html_e( 'Gemini API Key', 'trendly-ai-post' ); ?></label></th>
          <td><input name="trendly_ai_post_options[gemini_api_key]" type="text" id="gemini_api_key" value="<?php echo esc_attr( $opts['gemini_api_key'] ?? '' ); ?>" class="regular-text" /></td>
        </tr>
        <tr>
          <th scope="row"><label for="pexels_api_key"><?php esc_html_e( 'Pexels API Key', 'trendly-ai-post' ); ?></label></th>
          <td><input name="trendly_ai_post_options[pexels_api_key]" type="text" id="pexels_api_key" value="<?php echo esc_attr( $opts['pexels_api_key'] ?? '' ); ?>" class="regular-text" /></td>
        </tr>
        <tr>
          <th scope="row"><label for="region"><?php esc_html_e( 'Region (Google News)', 'trendly-ai-post' ); ?></label></th>
          <td>
            <select name="trendly_ai_post_options[region]" id="region">
              <?php foreach ( $regions as $code => $label ) : ?>
                <option value="<?php echo esc_attr( $code ); ?>" <?php selected( $opts['region'] ?? 'IN', $code ); ?>><?php echo esc_html( $label ); ?></option>
              <?php endforeach; ?>
            </select>
          </td>
        </tr>
        <tr>
          <th scope="row"><?php esc_html_e( 'Default Post Length', 'trendly-ai-post' ); ?></th>
          <td>
            <select name="trendly_ai_post_options[default_length]">
              <option value="short" <?php selected( $opts['default_length'] ?? '', 'short' ); ?>><?php esc_html_e( 'Short', 'trendly-ai-post' ); ?></option>
              <option value="medium" <?php selected( $opts['default_length'] ?? 'medium', 'medium' ); ?>><?php esc_html_e( 'Medium', 'trendly-ai-post' ); ?></option>
              <option value="long" <?php selected( $opts['default_length'] ?? '', 'long' ); ?>><?php esc_html_e( 'Long', 'trendly-ai-post' ); ?></option>
              <option value="extra" <?php selected( $opts['default_length'] ?? '', 'extra' ); ?>><?php esc_html_e( 'Extra-Long', 'trendly-ai-post' ); ?></option>
            </select>
          </td>
        </tr>
        <tr>
          <th scope="row"><?php esc_html_e( 'Default Category', 'trendly-ai-post' ); ?></th>
          <td><?php wp_dropdown_categories( [ 'hide_empty' => 0, 'name' => 'trendly_ai_post_options[default_category]', 'selected' => intval( $opts['default_category'] ?? 0 ) ] ); ?></td>
        </tr>
        <tr>
          <th scope="row"><?php esc_html_e( 'Default Author', 'trendly-ai-post' ); ?></th>
          <td><?php wp_dropdown_users( [ 'name' => 'trendly_ai_post_options[default_author]', 'selected' => intval( $opts['default_author'] ?? get_current_user_id() ) ] ); ?></td>
        </tr>
        <tr>
          <th scope="row"><?php esc_html_e( 'Auto-Schedule', 'trendly-ai-post' ); ?></th>
          <td>
            <label><input type="checkbox" name="trendly_ai_post_options[autoschedule_enabled]" value="1" <?php checked( ! empty( $opts['autoschedule_enabled'] ) ); ?>/> <?php esc_html_e( 'Enable automatic posting', 'trendly-ai-post' ); ?></label>
            <p class="description"><?php esc_html_e( 'When enabled, the plugin will generate 1-2 posts per day at your chosen time(s).', 'trendly-ai-post' ); ?></p>
          </td>
        </tr>
        <tr>
          <th scope="row"><?php esc_html_e( 'Posts Per Day', 'trendly-ai-post' ); ?></th>
          <td>
            <select name="trendly_ai_post_options[autoschedule_frequency]">
              <option value="daily-1" <?php selected( $opts['autoschedule_frequency'] ?? '', 'daily-1' ); ?>><?php esc_html_e( '1 per day', 'trendly-ai-post' ); ?></option>
              <option value="daily-2" <?php selected( $opts['autoschedule_frequency'] ?? '', 'daily-2' ); ?>><?php esc_html_e( '2 per day', 'trendly-ai-post' ); ?></option>
            </select>
          </td>
        </tr>
        <tr>
          <th scope="row"><?php esc_html_e( 'First Post Time', 'trendly-ai-post' ); ?></th>
          <td><input name="trendly_ai_post_options[autoschedule_time]" type="time" value="<?php echo esc_attr( $opts['autoschedule_time'] ?? '09:00' ); ?>" /></td>
        </tr>
      </tbody>
    </table>
    <?php submit_button(); ?>
  </form>

  <hr/>
  <h2><?php esc_html_e( 'Quick Actions', 'trendly-ai-post' ); ?></h2>
  <p>
    <button id="trendly-test-keys" class="button"><?php esc_html_e( 'Test API Keys', 'trendly-ai-post' ); ?></button>
    <button id="trendly-generate-draft" class="button button-primary"><?php esc_html_e( 'Generate Draft From Trend', 'trendly-ai-post' ); ?></button>
  </p>
  <div id="trendly-admin-result" style="margin-top:10px;"></div>
</div>
