<?php
const CEGIC_POSTS_FILE = __DIR__ . '/../data/posts.json';
const CEGIC_UPLOAD_DIR = __DIR__ . '/../uploads/posts';
const CEGIC_UPLOAD_URL = '../uploads/posts/';

$CEGIC_ACCOUNTS = [
    'cultura' => [
        'password' => 'cultura2026#1ce',
        'name' => 'Cultura Ministerial CEGIC',
        'icon' => 'fa-church'
    ],
    'educacion' => [
        'password' => 'educacion2026@1ce',
        'name' => 'Educaci¨®n Cristiana',
        'icon' => 'fa-graduation-cap'
    ],
    'conajuve' => [
        'password' => 'conajuveCeg.1',
        'name' => 'Conajuve - Juventud CEGIC',
        'icon' => 'fa-users'
    ],
    'conafem' => [
        'password' => 'conafem@.com1',
        'name' => 'Conafem',
        'icon' => 'fa-female'
    ],
    'amic' => [
        'password' => 'amicMisra.1',
        'name' => 'Agencia AMIC',
        'icon' => 'fa-hand-holding-heart'
    ],
    'tv' => [
        'password' => 'Rcegic.con@',
        'name' => 'TV/Radio CEGIC',
        'icon' => 'fa-tv'
    ],
    'social' => [
        'password' => 'Social.com1',
        'name' => 'Proyecci¨®n Social',
        'icon' => 'fa-hands-helping'
    ],
    'doctrina' => [
        'password' => 'doctrina.@123',
        'name' => 'Nuestra Doctrina',
        'icon' => 'fa-book-bible'
    ],
    'consejos' => [
        'password' => 'consejosRcegic1',
        'name' => 'Consejos Regionales',
        'icon' => 'fa-map-marked-alt'
    ],
    'datos' => [
        'password' => 'Estadis.com12',
        'name' => 'Datos Estad¨ªsticos',
        'icon' => 'fa-chart-bar'
    ],
    'contactos' => [
        'password' => 'contactos123@',
        'name' => 'Contactos',
        'icon' => 'fa-envelope'
    ],
    'admin' => [
        'password' => 'AdminCEGIC2026.Com1',
        'name' => 'Administrador General',
        'icon' => 'fa-user-shield',
        'role' => 'admin'
    ]
];

function cegic_json($data, $status = 200) {
    http_response_code($status);
    header('Content-Type: application/json; charset=utf-8');
    echo json_encode($data, JSON_UNESCAPED_UNICODE);
    exit;
}

function cegic_posts() {
    if (!file_exists(CEGIC_POSTS_FILE)) {
        return [];
    }

    $json = file_get_contents(CEGIC_POSTS_FILE);
    $posts = json_decode($json, true);

    return is_array($posts) ? $posts : [];
}

function cegic_save_posts($posts) {
    $dir = dirname(CEGIC_POSTS_FILE);

    if (!is_dir($dir)) {
        mkdir($dir, 0755, true);
    }

    file_put_contents(
        CEGIC_POSTS_FILE,
        json_encode($posts, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT)
    );
}

if (session_status() !== PHP_SESSION_ACTIVE) {
    session_start();
}