<?php
/**
 * DALTON PANEL - Yardımcı Fonksiyonlar
 */

/**
 * Güvenli input temizleme
 */
if (!function_exists('clean')) {
    function clean($data) {
        if ($data === null) return '';
        return htmlspecialchars(trim($data), ENT_QUOTES, 'UTF-8');
    }
}

/**
 * JSON yanıt döndürme
 */
if (!function_exists('jsonResponse')) {
    function jsonResponse($status, $message = '', $data = []) {
        header('Content-Type: application/json; charset=utf-8');
        echo json_encode([
            'sonuc' => $status,
            'mesaj' => $message,
            'data' => $data
        ], JSON_UNESCAPED_UNICODE);
        exit;
    }
}

/**
 * IP adresi alma
 */
if (!function_exists('getIP')) {
    function getIP() {
        if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
            return $_SERVER['HTTP_CLIENT_IP'];
        } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ips = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
            return trim($ips[0]);
        }
        return $_SERVER['REMOTE_ADDR'] ?? '0.0.0.0';
    }
}

/**
 * Türkçe karakter düzeltme (URL için)
 */
if (!function_exists('slugify')) {
    function slugify($text) {
        $turkce = ['ç', 'Ç', 'ğ', 'Ğ', 'ı', 'İ', 'ö', 'Ö', 'ş', 'Ş', 'ü', 'Ü', ' '];
        $latin = ['c', 'c', 'g', 'g', 'i', 'i', 'o', 'o', 's', 's', 'u', 'u', '-'];
        $text = str_replace($turkce, $latin, $text);
        $text = preg_replace('/[^a-zA-Z0-9\-]/', '', $text);
        return strtolower($text);
    }
}

/**
 * Rastgele kod üretme
 */
if (!function_exists('generateCode')) {
    function generateCode($length = 8) {
        $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789';
        $code = '';
        for ($i = 0; $i < $length; $i++) {
            $code .= $chars[random_int(0, strlen($chars) - 1)];
        }
        return $code;
    }
}

/**
 * Tarih formatla
 */
if (!function_exists('formatDate')) {
    function formatDate($date, $format = 'd/m/Y H:i:s') {
        return date($format, strtotime($date));
    }
}

/**
 * Para formatla
 */
if (!function_exists('formatMoney')) {
    function formatMoney($amount) {
        return number_format($amount, 0, ',', '.') . ' TL';
    }
}

/**
 * Platform ikonları
 */
if (!function_exists('getPlatformIcon')) {
    function getPlatformIcon($platform) {
        $icons = [
            'sahibinden' => 'https://is1-ssl.mzstatic.com/image/thumb/Purple116/v4/5d/72/c8/5d72c896-c46e-9dad-e32b-65bcc7344d36/AppIcon-0-0-1x_U007emarketing-0-6-0-0-85-220.png/146x0w.webp',
            'dolap' => 'https://is1-ssl.mzstatic.com/image/thumb/Purple126/v4/a0/36/89/a03689e9-8590-6077-2701-c61c7281eaad/AppIcon-1x_U007emarketing-0-7-0-85-220.png/230x0w.webp',
            'letgo' => 'https://is1-ssl.mzstatic.com/image/thumb/Purple116/v4/fe/fe/52/fefe52b3-0bbe-3c5e-f711-9df4bf618cee/AppIcon-0-0-1x_U007emarketing-0-0-0-7-0-0-sRGB-0-0-0-GLES2_U002c0-512MB-85-220-0-0.png/230x0w.webp',
            'pttavm' => 'https://is1-ssl.mzstatic.com/image/thumb/Purple126/v4/fc/1b/d9/fc1bd965-3f7c-78c7-90c4-3d1c20567316/AppIcon-0-0-1x_U007emarketing-0-10-0-0-85-220.png/230x0w.webp',
            'turkcell' => 'https://is1-ssl.mzstatic.com/image/thumb/Purple116/v4/79/41/63/794163e8-5793-bc19-988e-23fc4c41eedd/AppIconPrimary-0-0-1x_U007emarketing-0-0-0-7-0-0-sRGB-0-0-0-GLES2_U002c0-512MB-85-220-0-0.png/230x0w.webp',
            'shopier' => 'https://is1-ssl.mzstatic.com/image/thumb/Purple126/v4/88/a2/27/88a2277b-fc32-c7dd-0455-08f78b2b463c/AppIcon-0-0-1x_U007emarketing-0-0-0-7-0-0-sRGB-0-0-0-GLES2_U002c0-512MB-85-220-0-0.png/230x0w.webp',
            'yurtici' => 'https://www.yurticikargo.com/images/favicon.ico'
        ];
        return $icons[strtolower($platform)] ?? '';
    }
}

/**
 * Platform renkleri
 */
if (!function_exists('getPlatformColor')) {
    function getPlatformColor($platform) {
        $colors = [
            'sahibinden' => '#FF6B00',
            'dolap' => '#E91E63',
            'letgo' => '#00BCD4',
            'pttavm' => '#FFC107',
            'turkcell' => '#FFD600',
            'shopier' => '#9C27B0',
            'yurtici' => '#00796B'
        ];
        return $colors[strtolower($platform)] ?? '#FF8C00';
    }
}