<?php
require_once __DIR__ . '/../app/bootstrap.php';
use App\Core\Database; use App\Core\Security;
$errors=[];
if($_SERVER['REQUEST_METHOD']==='POST'){
 if(!verify_csrf()) $errors[]='Invalid security token.';
 if(!Security::rateLimit('contact:'.Security::clientIp(),5,3600)) $errors[]='Too many messages were submitted. Try again later.';
 $name=mb_substr(trim((string)($_POST['name']??'')),0,150);
 $email=strtolower(mb_substr(trim((string)($_POST['email']??'')),0,150));
 $phone=mb_substr(trim((string)($_POST['phone']??'')),0,60);
 $message=mb_substr(trim((string)($_POST['message']??'')),0,5000);
 if($name===''||mb_strlen($message)<5)$errors[]='Name and a meaningful message are required.';
 if($email!==''&&!filter_var($email,FILTER_VALIDATE_EMAIL))$errors[]='Invalid email.';
 if($phone!==''&&!preg_match('/^[0-9+() -]{6,30}$/',$phone))$errors[]='Invalid phone number.';
 if(!$errors){
  if(!Database::tableExists('contact_messages')){$errors[]='Contact storage is not installed. Import install/database_updates_v1.sql.';}
  else{Database::execute('INSERT INTO contact_messages (name,email,phone,message,status,created_at) VALUES (?,?,?,? ,"new",NOW())',[$name,$email?:null,$phone?:null,$message]);Security::logActivity(null,'contact_message_submitted',['email'=>$email]);set_flash('success','Your message has been received.');redirect('public/contact.php');}
 }
}
$pageTitle='Contact | '.BRAND_NAME; require ROOT_PATH.'/app/Layouts/frontend_header.php';
?>
<section class="container py-5"><h1 class="section-title">Contact <?= e(BRAND_NAME) ?></h1><?php if($m=flash('success')): ?><div class="alert alert-success"><?= e($m) ?></div><?php endif; ?><?php foreach($errors as $e1): ?><div class="alert alert-danger"><?= e($e1) ?></div><?php endforeach; ?><div class="row g-4"><div class="col-lg-6"><form method="post" class="card border-0 shadow-sm"><div class="card-body p-4"><?= csrf_field() ?><input class="form-control mb-3" name="name" maxlength="150" value="<?= e($_POST['name']??'') ?>" placeholder="Name" required><input class="form-control mb-3" type="email" name="email" maxlength="150" value="<?= e($_POST['email']??'') ?>" placeholder="Email"><input class="form-control mb-3" name="phone" maxlength="60" value="<?= e($_POST['phone']??'') ?>" placeholder="Phone"><textarea class="form-control mb-3" name="message" maxlength="5000" rows="6" placeholder="Message" required><?= e($_POST['message']??'') ?></textarea><button class="btn btn-primary">Send Message</button></div></form></div><div class="col-lg-6"><div class="landing-hero p-4 h-100"><h4>Customer support</h4><p>Address, social links and support channels can be managed from the admin settings.</p><p class="mb-0">WhatsApp: <?= e(setting('whatsapp_number','')) ?></p></div></div></div></section><?php require ROOT_PATH.'/app/Layouts/frontend_footer.php'; ?>
