<?php
/**
 * Plugin Name: Repair Café Heinsberg/Dremmen Site
 * Description: Lokale Erweiterungen für die Repaircafe-Heinsberg/Dremmen-Website: Design, Logo, Helfer-Formular und Anfragenverwaltung.
 * Version: 1.0.0
 * Author: Flatlogic AI
 */

if (!defined('ABSPATH')) {
    exit;
}

final class RCH_Site_Plugin {
    const VERSION = '1.2.5';
    const CPT = 'rch_volunteer';

    public static function init(): void {
        add_action('init', [__CLASS__, 'register_post_type']);
        add_action('wp_enqueue_scripts', [__CLASS__, 'enqueue_assets']);
        add_shortcode('rch_helper_form', [__CLASS__, 'helper_form_shortcode']);
        add_shortcode('rch_contact_box', [__CLASS__, 'contact_box_shortcode']);
        add_filter('manage_' . self::CPT . '_posts_columns', [__CLASS__, 'volunteer_columns']);
        add_action('manage_' . self::CPT . '_posts_custom_column', [__CLASS__, 'volunteer_column_content'], 10, 2);
        add_filter('wp_get_attachment_url', [__CLASS__, 'frontend_relative_attachment_url']);
        add_action('wp_head', [__CLASS__, 'disable_platform_badge'], 0);
    }

    public static function register_post_type(): void {
        register_post_type(self::CPT, [
            'labels' => [
                'name' => __('Helfer-Anfragen', 'rch'),
                'singular_name' => __('Helfer-Anfrage', 'rch'),
                'menu_name' => __('Helfer-Anfragen', 'rch'),
                'add_new_item' => __('Neue Helfer-Anfrage', 'rch'),
                'edit_item' => __('Helfer-Anfrage bearbeiten', 'rch'),
            ],
            'public' => false,
            'show_ui' => true,
            'show_in_menu' => true,
            'menu_icon' => 'dashicons-hammer',
            'supports' => ['title', 'editor', 'custom-fields'],
            'capability_type' => 'post',
        ]);
    }



    public static function disable_platform_badge(): void {
        if (is_admin()) {
            return;
        }
        echo "<script>window.FL_SHOW_BUDGE=false;</script>
";
    }

    public static function frontend_relative_attachment_url(string $url): string {
        if (is_admin()) {
            return $url;
        }
        if (preg_match('#^https?://(?:localhost|127\.0\.0\.1)(?::\d+)?/#', $url)) {
            return wp_make_link_relative($url);
        }
        return $url;
    }

    public static function enqueue_assets(): void {
        wp_register_style('rch-site-style', false, [], self::VERSION);
        wp_enqueue_style('rch-site-style');
        wp_add_inline_style('rch-site-style', self::css());
    }

    private static function css(): string {
        return <<<CSS
:root {
  --rch-green: #177245;
  --rch-green-dark: #0f4e32;
  --rch-mint: #e7f5ed;
  --rch-yellow: #ffd166;
  --rch-orange: #f97316;
  --rch-ink: #18211c;
  --rch-muted: #5f6f66;
  --rch-card: #ffffff;
  --rch-border: rgba(23, 114, 69, 0.16);
  --rch-shadow: 0 18px 45px rgba(15, 78, 50, 0.13);
}
.rch-site-header {
  position: sticky;
  top: 0;
  z-index: 50;
  backdrop-filter: blur(16px);
  background: rgba(255, 255, 255, .86);
  border-bottom: 1px solid rgba(23,114,69,.12);
}
.rch-site-header .wp-block-site-title a {
  color: var(--rch-green-dark);
  font-weight: 900;
  letter-spacing: -0.035em;
  text-decoration: none;
}
.rch-site-header .wp-block-navigation a {
  font-weight: 700;
  text-decoration: none;
}

.rch-site-header .wp-block-site-logo img {
  width: min(240px, 54vw);
  height: auto;
}
.rch-logo-link {
  display: inline-flex;
  align-items: center;
  line-height: 0;
  text-decoration: none;
}
.rch-site-header .rch-header-logo,
.rch-site-footer .rch-footer-logo {
  display: block;
  height: auto;
}
.rch-site-header .rch-header-logo {
  width: min(240px, 54vw);
}
.rch-site-footer .rch-footer-logo {
  width: min(230px, 60vw);
  margin-bottom: 1rem;
}
.rch-brand-row {
  display: flex;
  flex-wrap: wrap;
  gap: clamp(18px, 4vw, 38px);
  align-items: center;
}
.rch-brand-logo {
  width: min(360px, 72vw);
  height: auto;
  filter: drop-shadow(0 14px 22px rgba(15,78,50,.13));
}
.rch-hero-content {
  min-width: min(100%, 540px);
  flex: 1 1 560px;
}
.rch-hero-logo-panel {
  flex: 0 1 360px;
  padding: clamp(18px, 4vw, 28px);
  border-radius: 28px;
  background: rgba(255,255,255,.72);
  border: 1px solid var(--rch-border);
  box-shadow: 0 14px 36px rgba(15,78,50,.10);
}
.rch-fact-list {
  list-style: none;
  padding: 0;
  margin: 18px 0 0;
}
.rch-fact-list li {
  display: flex;
  gap: .7rem;
  padding: .68rem 0;
  border-bottom: 1px solid rgba(23,114,69,.12);
}
.rch-fact-list strong {
  color: var(--rch-green-dark);
  min-width: 92px;
}
.rch-contact-card a[href^="tel:"],
.rch-contact-card a[href^="mailto:"] {
  font-weight: 800;
}
.rch-site-header .wp-block-navigation a[href*="helfer-werden"] {
  background: var(--rch-green);
  color: #fff !important;
  border-radius: 999px;
  padding: .62rem .9rem;
}
.rch-site-header .wp-block-navigation a[href*="helfer-werden"]:hover {
  background: var(--rch-green-dark);
}
.rch-site-footer {
  margin-top: clamp(48px, 7vw, 90px);
  background: #0f2f22;
  color: #fff;
}
.rch-site-footer a { color: #fff; }
.rch-site-footer p,
.rch-site-footer .wp-block-site-tagline { color: rgba(255,255,255,.76); }
.rch-site-footer .wp-block-site-title a {
  color: #fff;
  font-weight: 900;
  text-decoration: none;
}
body {
  color: var(--rch-ink);
  background: linear-gradient(180deg, #fbfff9 0%, #ffffff 38%, #f5fbf7 100%);
}
.wp-site-blocks > main,
.entry-content {
  overflow: hidden;
}
.rch-wrap {
  max-width: 1120px;
  margin-inline: auto;
  padding-inline: clamp(18px, 4vw, 36px);
}
.rch-wrap *,
.rch-wrap *::before,
.rch-wrap *::after,
.rch-site-header *,
.rch-site-header *::before,
.rch-site-header *::after,
.rch-site-footer *,
.rch-site-footer *::before,
.rch-site-footer *::after {
  box-sizing: border-box;
}
.rch-section {
  padding-block: clamp(42px, 7vw, 88px);
}
.rch-hero {
  position: relative;
  isolation: isolate;
  border-radius: 34px;
  margin: clamp(18px, 4vw, 36px) auto clamp(28px, 5vw, 64px);
  padding: clamp(42px, 8vw, 92px) clamp(22px, 6vw, 72px);
  background:
    radial-gradient(circle at 86% 16%, rgba(255, 209, 102, .70), transparent 27%),
    radial-gradient(circle at 8% 76%, rgba(23, 114, 69, .18), transparent 24%),
    linear-gradient(135deg, #eaf8ef 0%, #fffaf0 54%, #ffffff 100%);
  box-shadow: var(--rch-shadow);
  border: 1px solid var(--rch-border);
}
.rch-hero:before {
  content: "";
  position: absolute;
  inset: auto 28px 28px auto;
  width: min(30vw, 310px);
  aspect-ratio: 1;
  border-radius: 50%;
  background: repeating-linear-gradient(135deg, rgba(23,114,69,.16) 0 10px, rgba(255,255,255,.42) 10px 20px);
  z-index: -1;
}
.rch-eyebrow {
  display: inline-flex;
  align-items: center;
  gap: .5rem;
  padding: .42rem .78rem;
  border-radius: 999px;
  background: rgba(255,255,255,.78);
  border: 1px solid var(--rch-border);
  color: var(--rch-green-dark);
  font-weight: 700;
  letter-spacing: .015em;
}
.rch-hero h1,
.rch-page-title {
  margin: 18px 0 18px;
  max-width: 850px;
  line-height: .98;
  font-size: clamp(2.55rem, 7vw, 5.65rem);
  letter-spacing: -0.055em;
}
.rch-lead {
  max-width: 760px;
  color: var(--rch-muted);
  font-size: clamp(1.08rem, 2vw, 1.34rem);
  line-height: 1.6;
}
.rch-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  margin-top: 28px;
}
.rch-button,
.rch-button-outline,
.rch-helper-form button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: .45rem;
  min-height: 48px;
  padding: 0.85rem 1.15rem;
  border-radius: 999px;
  font: inherit;
  font-size: 1rem;
  line-height: 1.2;
  font-weight: 800;
  text-align: center;
  cursor: pointer;
  text-decoration: none !important;
  transition: transform .18s ease, box-shadow .18s ease, background .18s ease;
}
.rch-button,
.rch-helper-form button {
  border: 0;
  color: #fff !important;
  background: var(--rch-green);
  box-shadow: 0 12px 26px rgba(23,114,69,.24);
}
.rch-button:hover,
.rch-helper-form button:hover {
  transform: translateY(-2px);
  background: var(--rch-green-dark);
}
.rch-button-outline {
  color: var(--rch-green-dark) !important;
  background: rgba(255,255,255,.78);
  border: 1px solid var(--rch-border);
}
.rch-button-outline:hover {
  transform: translateY(-2px);
  box-shadow: 0 10px 22px rgba(15,78,50,.10);
}
.rch-stat-row,
.rch-grid,
.rch-two-col,
.rch-steps {
  display: grid;
  gap: clamp(16px, 3vw, 28px);
}
.rch-stat-row {
  grid-template-columns: repeat(3, minmax(0, 1fr));
  margin-top: clamp(28px, 5vw, 54px);
}
.rch-stat,
.rch-card,
.rch-form-card,
.rch-contact-card,
.rch-step,
.rch-event-card {
  background: rgba(255,255,255,.90);
  border: 1px solid var(--rch-border);
  border-radius: 24px;
  box-shadow: 0 12px 35px rgba(15, 78, 50, .08);
}
.rch-stat {
  padding: 22px;
}
.rch-stat strong {
  display: block;
  color: var(--rch-green-dark);
  font-size: clamp(1.65rem, 4vw, 2.7rem);
  line-height: 1;
}
.rch-stat span {
  color: var(--rch-muted);
}
.rch-grid {
  grid-template-columns: repeat(3, minmax(0, 1fr));
}
.rch-two-col {
  grid-template-columns: minmax(0, 1.1fr) minmax(280px, .9fr);
  align-items: start;
}
.rch-card,
.rch-contact-card,
.rch-event-card {
  padding: clamp(22px, 4vw, 34px);
}
.rch-card h3,
.rch-step h3,
.rch-event-card h3 {
  margin-top: 0;
  margin-bottom: 10px;
  letter-spacing: -0.025em;
}
.rch-icon {
  width: 50px;
  height: 50px;
  display: inline-grid;
  place-items: center;
  border-radius: 16px;
  background: var(--rch-mint);
  color: var(--rch-green-dark);
  font-size: 1.55rem;
  margin-bottom: 16px;
}
.rch-band {
  border-radius: 30px;
  background:
    linear-gradient(135deg, rgba(23,114,69,.94), rgba(15,78,50,.98)),
    radial-gradient(circle at 100% 0%, rgba(255,209,102,.45), transparent 36%);
  color: #fff;
  padding: clamp(30px, 6vw, 58px);
  box-shadow: var(--rch-shadow);
}
.rch-band p,
.rch-band li {
  color: rgba(255,255,255,.86);
}
.rch-band .rch-button-outline {
  color: #fff !important;
  border-color: rgba(255,255,255,.38);
  background: rgba(255,255,255,.12);
}
.rch-steps {
  grid-template-columns: repeat(4, minmax(0, 1fr));
  counter-reset: rch-step;
}
.rch-step {
  padding: 24px;
  position: relative;
}
.rch-step:before {
  counter-increment: rch-step;
  content: counter(rch-step);
  width: 36px;
  height: 36px;
  display: grid;
  place-items: center;
  border-radius: 999px;
  background: var(--rch-yellow);
  color: var(--rch-ink);
  font-weight: 900;
  margin-bottom: 16px;
}
.rch-list {
  padding-left: 1.1rem;
}
.rch-list li {
  margin-bottom: .52rem;
}
.rch-note {
  border-left: 5px solid var(--rch-yellow);
  background: #fff9e8;
  padding: 18px 20px;
  border-radius: 16px;
}
.rch-helper-form label {
  display: block;
  margin-bottom: 14px;
  color: var(--rch-ink);
  font-weight: 700;
}
.rch-helper-form input,
.rch-helper-form textarea,
.rch-helper-form select {
  width: 100%;
  max-width: 100%;
  box-sizing: border-box;
  margin-top: 7px;
  border: 1px solid rgba(24,33,28,.18);
  border-radius: 14px;
  padding: 12px 14px;
  font: inherit;
  background: #fff;
}
.rch-helper-form textarea {
  min-height: 125px;
  resize: vertical;
}
.rch-helper-form .rch-checkbox {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  font-weight: 500;
  color: var(--rch-muted);
}
.rch-helper-form .rch-checkbox input {
  width: auto;
  margin-top: 5px;
}
.rch-form-card {
  padding: clamp(22px, 5vw, 38px);
}
.rch-success,
.rch-error {
  padding: 16px 18px;
  border-radius: 16px;
  margin-bottom: 18px;
  font-weight: 700;
}
.rch-success {
  background: #e9f8ee;
  color: #0d5d36;
  border: 1px solid rgba(13,93,54,.2);
}
.rch-error {
  background: #fff0ee;
  color: #9f1d1d;
  border: 1px solid rgba(159,29,29,.2);
}
.rch-small {
  color: var(--rch-muted);
  font-size: .94rem;
}
.rch-kicker {
  color: var(--rch-green-dark);
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: .08em;
  font-size: .78rem;
}
.rch-faq details {
  border-bottom: 1px solid var(--rch-border);
  padding: 18px 0;
}
.rch-faq summary {
  cursor: pointer;
  font-weight: 800;
  color: var(--rch-green-dark);
}
.rch-footer-cta {
  margin-block: clamp(36px, 6vw, 72px);
}
@media (max-width: 1080px) {
  .rch-site-header .alignwide {
    padding-inline: clamp(16px, 4vw, 28px);
  }
  .rch-site-header .wp-block-group.alignwide {
    gap: 16px;
  }
  .rch-site-header .wp-block-navigation {
    flex: 1 1 auto;
  }
  .rch-site-header .wp-block-navigation__container {
    column-gap: 12px;
    row-gap: 8px;
  }
  .rch-grid,
  .rch-stat-row {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .rch-steps {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
}
@media (max-width: 860px) {
  .rch-grid,
  .rch-stat-row,
  .rch-two-col,
  .rch-steps {
    grid-template-columns: 1fr;
  }
  .rch-hero h1,
  .rch-page-title {
    letter-spacing: -0.04em;
  }
  .rch-site-header {
    position: sticky;
  }
  .rch-site-header .wp-block-group.alignwide {
    align-items: center;
    flex-wrap: nowrap !important;
  }
  .rch-site-header .wp-block-site-logo img {
    width: min(170px, 48vw);
  }
  .rch-header-title {
    display: none;
  }
  .rch-brand-row {
    display: grid;
    grid-template-columns: 1fr;
  }
  .rch-hero-logo-panel {
    order: -1;
    justify-self: center;
    max-width: min(100%, 310px);
  }
  .rch-brand-logo {
    width: min(290px, 70vw);
  }
  .rch-site-footer .wp-block-columns {
    gap: 28px;
  }
}
@media (max-width: 640px) {
  html {
    -webkit-text-size-adjust: 100%;
  }
  body {
    overflow-x: hidden;
  }
  .wp-site-blocks {
    width: 100%;
    overflow-x: clip;
  }
  .rch-wrap {
    padding-inline: 16px;
  }
  .rch-section {
    padding-block: 34px;
  }
  .rch-hero {
    border-radius: 24px;
    margin-block: 14px 26px;
    padding: 28px 18px;
  }
  .rch-hero:before {
    opacity: .45;
    inset: auto 14px 14px auto;
    width: 180px;
  }
  .rch-eyebrow {
    max-width: 100%;
    white-space: normal;
    line-height: 1.3;
  }
  .rch-hero h1,
  .rch-page-title {
    font-size: clamp(2rem, 13vw, 3.05rem);
    line-height: 1.03;
    margin-block: 14px;
  }
  .rch-lead {
    font-size: 1.03rem;
    line-height: 1.55;
  }
  .rch-actions {
    display: grid;
    grid-template-columns: 1fr;
    gap: 10px;
    margin-top: 22px;
  }
  .rch-button,
  .rch-button-outline,
  .rch-helper-form button {
    width: 100%;
    min-height: 52px;
    padding-inline: 1rem;
    text-align: center;
    white-space: normal;
  }
  .rch-card,
  .rch-contact-card,
  .rch-event-card,
  .rch-stat,
  .rch-step,
  .rch-form-card {
    border-radius: 20px;
    padding: 20px;
  }
  .rch-band {
    border-radius: 22px;
    padding: 26px 20px;
  }
  .rch-fact-list li {
    display: grid;
    gap: .25rem;
  }
  .rch-fact-list strong {
    min-width: 0;
  }
  .rch-contact-card a,
  .rch-card a,
  .rch-band a,
  .rch-site-footer a {
    overflow-wrap: anywhere;
    word-break: break-word;
  }
  .rch-helper-form label {
    margin-bottom: 16px;
  }
  .rch-helper-form input,
  .rch-helper-form textarea,
  .rch-helper-form select {
    min-height: 48px;
    font-size: 16px;
    border-radius: 12px;
  }
  .rch-helper-form textarea {
    min-height: 140px;
  }
  .rch-helper-form .rch-checkbox {
    align-items: flex-start;
  }
  .rch-helper-form .rch-checkbox input {
    flex: 0 0 auto;
    margin-top: 3px;
  }
  .rch-site-header {
    background: rgba(255,255,255,.96);
  }
  .rch-site-header > .wp-block-group,
  .rch-site-header .wp-block-group.alignwide {
    width: 100%;
  }
  .rch-site-header .wp-block-group.alignwide > .wp-block-group:first-child {
    min-width: 0;
  }
  .rch-site-header .wp-block-site-logo img {
    width: min(150px, 46vw);
  }
  .rch-site-header .wp-block-navigation__responsive-container-open {
    border: 1px solid var(--rch-border);
    border-radius: 999px;
    padding: 9px 11px;
    background: #fff;
    color: var(--rch-green-dark);
  }
  .rch-site-header .wp-block-navigation__responsive-container.is-menu-open {
    position: fixed !important;
    inset: 0 !important;
    z-index: 99999 !important;
    width: 100vw !important;
    height: 100vh !important;
    height: 100dvh !important;
    max-width: none !important;
    padding: calc(20px + env(safe-area-inset-top)) 20px calc(26px + env(safe-area-inset-bottom)) !important;
    overflow-y: auto !important;
    box-sizing: border-box;
    background: #fff !important;
  }
  .rch-site-header .wp-block-navigation__responsive-container.is-menu-open .wp-block-navigation__responsive-close,
  .rch-site-header .wp-block-navigation__responsive-container.is-menu-open .wp-block-navigation__responsive-dialog {
    width: 100%;
    max-width: none;
    margin: 0;
  }
  .rch-site-header .wp-block-navigation__responsive-container.is-menu-open .wp-block-navigation__responsive-container-close {
    position: absolute;
    top: calc(20px + env(safe-area-inset-top));
    right: 20px;
    width: 44px;
    height: 44px;
    display: grid;
    place-items: center;
    border: 1px solid var(--rch-border);
    border-radius: 999px;
    background: #f6fbf8;
    color: var(--rch-green-dark);
  }
  .rch-site-header .wp-block-navigation__responsive-container.is-menu-open .wp-block-navigation__responsive-container-content {
    width: 100%;
    padding-top: 72px !important;
    margin: 0;
    align-items: stretch;
  }
  .rch-site-header .wp-block-navigation__responsive-container.is-menu-open .wp-block-navigation__container {
    align-items: stretch;
    width: 100%;
    gap: 10px;
  }
  .rch-site-header .wp-block-navigation__responsive-container.is-menu-open a {
    display: block;
    width: 100%;
    padding: 13px 14px;
    border-radius: 14px;
    background: #f6fbf8;
    color: var(--rch-green-dark) !important;
    font-weight: 850;
  }
  .rch-site-header .wp-block-navigation__responsive-container.is-menu-open a[href*="helfer-werden"] {
    text-align: center;
    background: var(--rch-green) !important;
    color: #fff !important;
  }
  .rch-site-footer {
    margin-top: 40px;
  }
  .rch-site-footer .wp-block-columns {
    display: block;
  }
  .rch-site-footer .wp-block-column + .wp-block-column {
    margin-top: 28px;
  }
}
@media (max-width: 420px) {
  .rch-wrap {
    padding-inline: 14px;
  }
  .rch-hero {
    padding: 24px 15px;
  }
  .rch-hero h1,
  .rch-page-title {
    font-size: clamp(1.85rem, 14vw, 2.6rem);
  }
  .rch-card,
  .rch-contact-card,
  .rch-event-card,
  .rch-stat,
  .rch-step,
  .rch-form-card {
    padding: 18px;
  }
  .rch-brand-logo {
    width: min(245px, 74vw);
  }
}
CSS;
    }

    public static function helper_form_shortcode(): string {
        $errors = [];
        $success = false;

        if ($_SERVER['REQUEST_METHOD'] === 'POST' && isset($_POST['rch_helper_form'])) {
            $success = self::handle_helper_form($errors);
        }

        ob_start();
        echo '<div class="rch-form-card">';
        if ($success) {
            echo '<div class="rch-success">Danke! Deine Anfrage wurde gespeichert. Wir melden uns bald bei dir.</div>';
        } elseif (!empty($errors)) {
            echo '<div class="rch-error">Bitte prüfe die markierten Angaben: ' . esc_html(implode(' ', $errors)) . '</div>';
        }
        ?>
        <form class="rch-helper-form" method="post">
            <?php wp_nonce_field('rch_helper_form_action', 'rch_helper_nonce'); ?>
            <input type="hidden" name="rch_helper_form" value="1">
            <label>Dein Name *
                <input type="text" name="rch_name" autocomplete="name" required value="<?php echo esc_attr($_POST['rch_name'] ?? ''); ?>">
            </label>
            <label>E-Mail-Adresse *
                <input type="email" name="rch_email" autocomplete="email" required value="<?php echo esc_attr($_POST['rch_email'] ?? ''); ?>">
            </label>
            <label>Telefon / Rückruf optional
                <input type="tel" name="rch_phone" autocomplete="tel" value="<?php echo esc_attr($_POST['rch_phone'] ?? ''); ?>">
            </label>
            <label>Wobei möchtest du helfen?
                <select name="rch_interest">
                    <?php
                    $options = ['Elektro & Elektronik', '3D-Druck / Ersatzteile', 'Textilien & Nähen', 'Fahrräder', 'Holz / Mechanik', 'Organisation & Empfang', 'Kaffee, Kuchen & Gästebetreuung', 'Noch offen'];
                    $selected = sanitize_text_field($_POST['rch_interest'] ?? '');
                    foreach ($options as $option) {
                        printf('<option value="%1$s" %2$s>%1$s</option>', esc_attr($option), selected($selected, $option, false));
                    }
                    ?>
                </select>
            </label>
            <label>Deine Nachricht
                <textarea name="rch_message" placeholder="Erzähl uns kurz von deinen Fähigkeiten, deiner Verfügbarkeit oder deinen Fragen."><?php echo esc_textarea($_POST['rch_message'] ?? ''); ?></textarea>
            </label>
            <label class="rch-checkbox">
                <input type="checkbox" name="rch_privacy" value="1" required <?php checked(!empty($_POST['rch_privacy'])); ?>>
                <span>Ich bin einverstanden, dass meine Angaben zur Bearbeitung meiner Anfrage gespeichert und per E-Mail an das Repair-Café-Team weitergeleitet werden. *</span>
            </label>
            <button type="submit">Anfrage senden</button>
            <p class="rch-small">Pflichtfelder sind mit * markiert. Die Anfrage erscheint zusätzlich im WordPress-Adminbereich unter „Helfer-Anfragen“.</p>
        </form>
        <?php
        echo '</div>';
        return ob_get_clean();
    }

    private static function handle_helper_form(array &$errors): bool {
        if (!isset($_POST['rch_helper_nonce']) || !wp_verify_nonce(sanitize_text_field(wp_unslash($_POST['rch_helper_nonce'])), 'rch_helper_form_action')) {
            $errors[] = 'Sicherheitsprüfung fehlgeschlagen.';
            return false;
        }

        $name = sanitize_text_field(wp_unslash($_POST['rch_name'] ?? ''));
        $email = sanitize_email(wp_unslash($_POST['rch_email'] ?? ''));
        $phone = sanitize_text_field(wp_unslash($_POST['rch_phone'] ?? ''));
        $interest = sanitize_text_field(wp_unslash($_POST['rch_interest'] ?? ''));
        $message = sanitize_textarea_field(wp_unslash($_POST['rch_message'] ?? ''));
        $privacy = !empty($_POST['rch_privacy']);

        if ($name === '') {
            $errors[] = 'Name fehlt.';
        }
        if ($email === '' || !is_email($email)) {
            $errors[] = 'E-Mail ist ungültig.';
        }
        if (!$privacy) {
            $errors[] = 'Datenschutzhinweis muss bestätigt werden.';
        }
        if (!empty($errors)) {
            return false;
        }

        $content = "Name: {$name}\nE-Mail: {$email}\nTelefon: {$phone}\nInteresse: {$interest}\n\nNachricht:\n{$message}";
        $post_id = wp_insert_post([
            'post_type' => self::CPT,
            'post_status' => 'private',
            'post_title' => sprintf('Helfer-Anfrage: %s', $name),
            'post_content' => $content,
            'meta_input' => [
                'rch_name' => $name,
                'rch_email' => $email,
                'rch_phone' => $phone,
                'rch_interest' => $interest,
            ],
        ], true);

        if (is_wp_error($post_id)) {
            $errors[] = 'Die Anfrage konnte nicht gespeichert werden.';
            return false;
        }

        $notification_email = 'reparatur@habmalnefrage.de';
        if (is_email($notification_email)) {
            wp_mail(
                $notification_email,
                'Neue Helfer-Anfrage für Repaircafe Heinsberg/Dremmen',
                $content,
                ['Reply-To: ' . $name . ' <' . $email . '>']
            );
        }

        $_POST = [];
        return true;
    }

    public static function contact_box_shortcode(): string {
        $email = antispambot('reparatur@habmalnefrage.de');
        return '<div class="rch-contact-card"><p class="rch-kicker">Kontakt</p><h3>Franz-Josef Michiels</h3><p>Für Fragen, Terminabsprachen oder Hinweise zu aufwändigeren Reparaturen erreichst du das Repaircafe Heinsberg/Dremmen direkt per E-Mail oder Telefon.</p><p><strong>E-Mail:</strong> <a href="mailto:reparatur@habmalnefrage.de">' . esc_html($email) . '</a><br><strong>Telefon:</strong> <a href="tel:+492452939717">02452 939717</a></p><p><strong>Ort:</strong><br>Keller Pfarrheim Dremmen<br>Lambertusstr. 27<br>52525 Heinsberg/Dremmen</p><p class="rch-small">Hinweis: Der Veranstaltungsort liegt im Keller und ist daher leider nicht barrierearm.</p></div>';
    }

    public static function volunteer_columns(array $columns): array {
        $columns['rch_email'] = __('E-Mail', 'rch');
        $columns['rch_interest'] = __('Interesse', 'rch');
        return $columns;
    }

    public static function volunteer_column_content(string $column, int $post_id): void {
        if ($column === 'rch_email') {
            echo esc_html(get_post_meta($post_id, 'rch_email', true));
        }
        if ($column === 'rch_interest') {
            echo esc_html(get_post_meta($post_id, 'rch_interest', true));
        }
    }
}

RCH_Site_Plugin::init();
