<% settings = local_assigns[:settings] || Setting.plugin_redmine_ux_toolkit || {} %>

<div class="ux-toolkit-settings-group">
  <header class="ux-toolkit-settings-group-header">
    <h3><%= l(:label_ux_toolkit_cron_monitoring) %></h3>
    <p><%= l(:text_ux_toolkit_cron_monitoring_help) %></p>
  </header>
  <div class="ux-toolkit-settings-group-body">
    <% monitoring_interval = settings.fetch('cron_monitoring_interval', '4_hours').to_s %>
    <% monitoring_email = settings.fetch('cron_monitoring_email', '').to_s %>
    <% redmine_sender_email = RedmineUxToolkit::Services::CronMonitor.notification_sender_email %>
    <% server_admin_email = RedmineUxToolkit::Services::CronMonitor.server_admin_email(request.env) %>
    <% effective_monitoring_email = RedmineUxToolkit::Services::CronMonitor.monitoring_recipient(
         settings,
         server_admin_email: server_admin_email,
         redmine_sender_email: redmine_sender_email
       ) %>

    <p>
      <%= label_tag 'settings_cron_monitoring_interval', l(:label_ux_toolkit_cron_monitoring_interval) %>
      <%= select_tag 'settings[cron_monitoring_interval]',
                     options_for_select([
                       [l(:label_ux_toolkit_cron_monitoring_none), 'none'],
                       [l(:label_ux_toolkit_cron_monitoring_after_four_hours), '4_hours']
                     ], monitoring_interval),
                     id: 'settings_cron_monitoring_interval' %>
    </p>

    <p>
      <%= label_tag 'settings_cron_monitoring_email', l(:label_ux_toolkit_cron_monitoring_email) %>
      <%= email_field_tag 'settings[cron_monitoring_email]', monitoring_email,
                          id: 'settings_cron_monitoring_email', size: 45 %>
      <% if redmine_sender_email.present? %>
        <em class="info"><%= l(:text_ux_toolkit_cron_monitoring_redmine_sender_advice, email: redmine_sender_email) %></em>
      <% elsif server_admin_email.present? %>
        <em class="info"><%= l(:text_ux_toolkit_cron_monitoring_server_admin_advice, email: server_admin_email) %></em>
      <% else %>
        <em class="info"><%= l(:text_ux_toolkit_cron_monitoring_email_fallback_help) %></em>
      <% end %>
    </p>

    <% if monitoring_interval != 'none' && monitoring_email.blank? && effective_monitoring_email.blank? %>
      <p class="warning"><%= l(:text_ux_toolkit_cron_monitoring_email_missing) %></p>
    <% elsif monitoring_interval != 'none' && !RedmineUxToolkit::Services::CronMonitor.email_valid?(monitoring_email) %>
      <% if monitoring_email.present? %>
        <p class="warning"><%= l(:text_ux_toolkit_cron_monitoring_email_invalid) %></p>
      <% end %>
    <% end %>

    <p class="info"><%= l(:text_ux_toolkit_cron_monitoring_browser_check_help) %></p>

    <% if RedmineUxToolkit::Services::CronMonitor.table_available? %>
      <% monitor_state = UxToolkitCronMonitorState.find_by(name: UxToolkitCronMonitorState::DEFAULT_NAME) %>
      <% if monitor_state %>
        <% monitor_status = {
             'healthy' => l(:label_ux_toolkit_cron_monitor_status_healthy),
             'alerted' => l(:label_ux_toolkit_cron_monitor_status_alerted),
             'email_failed' => l(:label_ux_toolkit_cron_monitor_status_email_failed),
             'check_failed' => l(:label_ux_toolkit_cron_monitor_status_check_failed)
           }.fetch(monitor_state.last_status.to_s, monitor_state.last_status.presence || '-') %>
        <table class="list">
          <tbody>
            <tr>
              <th><%= l(:label_ux_toolkit_cron_monitor_last_check) %></th>
              <td><%= monitor_state.last_checked_at ? format_time(monitor_state.last_checked_at) : '-' %></td>
            </tr>
            <tr>
              <th><%= l(:label_ux_toolkit_cron_monitor_last_alert) %></th>
              <td><%= monitor_state.last_alerted_at ? format_time(monitor_state.last_alerted_at) : '-' %></td>
            </tr>
            <tr>
              <th><%= l(:label_ux_toolkit_cron_status) %></th>
              <td><%= monitor_status %></td>
            </tr>
            <tr>
              <th><%= l(:label_ux_toolkit_cron_last_message) %></th>
              <td><%= monitor_state.last_message.presence || '-' %></td>
            </tr>
          </tbody>
        </table>
      <% end %>
    <% end %>
  </div>
</div>
