<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

use App\Custom\SQLConnection;

class Notificacion extends Model {
    /**
     * La ruta del directorio que contiene los archivos json de notificaciones
     * @access private
     * @static
     * @var string
     */
    private static $path;

    /**
     * Función que guarda las notificaciones del usuario
     * @access public
     * @static
     * @param int El id del usuario
     * @return void
     */
    public static function notificacionesBackend($user_id) {
        $nombre_json = self::crearJsonFile($user_id);
        self::guardarNotificacionJson($nombre_json, $user_id);

    }

    /**
     * Función que almacena los datos del store en un archivo json
     * @access private
     * @static
     * @param string El id del usuario
     * @return void
     */
    private static function guardarNotificacionJson($json_file_name, $user_id) {
        self::$path = public_path() . '/notifications';
        // $notificaciones = SQLConnection::backendStoreProcedure('LN_panel_reports',array('NOTIFAINICIAR', $user_id,NULL, null,env('PLATAFORMA'),null,env('PLATAFORMA'),NULL));
        $notificaciones = json_encode(SQLConnection::backendStoreProcedure('LN_PANEL_REPORTS', array('IVNOTIFJSON', $user_id)));
        file_put_contents(self::$path . '/' . $json_file_name, $notificaciones);
    }

    /**
     * Función que crea el archivo json o vacía su contenido si exste, y devuelve su nombre
     * @access private
     * @static
     * @param int El id del usuario
     * @return string El nombre del archivo json
     */
    private static function crearJsonFile($user_id) {
        self::$path = public_path() . '/notifications';
        $name_json = self::obtenerNombreJson($user_id);
        if (!file_exists(self::$path . '/' . $name_json)) {
            $archivo = fopen(self::$path . '/' . $name_json, 'w');
            fclose($archivo);
            chmod(self::$path . '/' . $name_json, 0777);
        } else {
            self::borrarContenido($name_json);
        }
        return $name_json;
    }

    /**
     * Función que borra el contenido del archivo json
     * @access private
     * @static
     * @param string El id del usuario
     * @return void
     */
    private static function borrarContenido($name_file) {
        self::$path = public_path() . '/notifications';
        if (file_exists(self::$path . '/' . $name_file)) {
            $archivo = fopen(self::$path . '/' . $name_file, 'w');
            fwrite($archivo, '');
            fclose($archivo);
        }
    }

    public static function eliminarJson($user_id) {
        self::$path = public_path() . '/notifications';
        $name_json = self::obtenerNombreJson($user_id);
        if (file_exists(self::$path . '/' . $name_json)) {
            unlink(self::$path . '/' . $name_json);
        }
    }

    private static function obtenerNombreJson($user_id) {
        return 'notificacion_' . hash('md5', $user_id) . '.json';
    }
}
