<?php

header("Access-Control-Allow-Origin: *");

header("Access-Control-Allow-Headers: Content-Type");

header("Access-Control-Allow-Methods: POST, OPTIONS");

header("Content-Type: application/json");

require_once "../config/database.php";

$data = json_decode(
  file_get_contents("php://input"),
  true
);

$clan = $data["clan"];
$enemyClan = $data["enemyClan"];

$format = $data["format"];

$date = $data["date"];
$time = $data["time"];

$mapName = $data["mapName"];

$point = $data["point"];

$rules = $data["rules"];

$comments = $data["comments"];

$sql = "INSERT INTO matches (

  clan,
  enemy_clan,

  format_type,

  match_date,
  match_time,

  map_name,

  dispute_point,

  rules,

  comments

) VALUES (

  ?, ?, ?, ?, ?, ?, ?, ?, ?

)";

$stmt = $conn->prepare($sql);

$stmt->bind_param(
  "sssssssss",

  $clan,
  $enemyClan,

  $format,

  $date,
  $time,

  $mapName,

  $point,

  $rules,

  $comments
);

$stmt->execute();

$webhook = "https://discord.com/api/webhooks/1503974710097084457/q5WXbLRbwOEvM9Inype8QBNgXjzcZydXDX8m6x1S_VccqElvOvAPAm6ZNhBGNFccnsyB";

$message = [
  "content" => "

🔥 NUEVO MATCH AGENDADO

⚔️ $clan vs $enemyClan

🗺️ MAPA: $mapName

🎯 PUNTO: $point

📅 FECHA: $date

⏰ HORA: $time

📜 FORMATO: $format

📌 REGLAS:
$rules

💬 COMENTARIOS:
$comments
"
];

$options = [
  "http" => [
    "header"  => "Content-type: application/json",
    "method"  => "POST",
    "content" => json_encode($message)
  ]
];

$context  = stream_context_create($options);

file_get_contents(
  $webhook,
  false,
  $context
);

echo json_encode([
  "success" => true
]);