<?php
include_once(ROOT . FOLDER_CORE . '/auth.php'); 
include_once(ROOT . FOLDER_CORE . '/class.php'); 
if($auth == 1) {
    if(isset($_POST['key_car']) && isset($_POST['file_name']) && isset($_POST['id_origin'])) {
        if(!empty($_POST['key_car']) && !empty($_POST['file_name'])  && !empty($_POST['id_origin'])) {
            $Schema = new Schema($bd_conn);
            $Bibliotecas = new Bibliotecas($bd_conn);
            $date = date('Y-m-d H:i:s');
            $imagenes = [];
            $key_car = Statics::SafeInput($bd_conn, $_POST['key_car']);
            $position_file = Statics::SafeInput($bd_conn, $_POST['position']);
            $file_name = Statics::SafeInput($bd_conn, $_POST['file_name']);
            $id_origin = Statics::SafeInput($bd_conn, $_POST['id_origin']);
            $orden = $Schema->executeQuery('SELECT ORDEN FROM VEHICULOS_MULTIMEDIA_LINKS WHERE VEHICULO =? ORDER BY ORDEN DESC', 'i', [$key_car])->fetch_array();
            $position = empty($orden['ORDEN']) ? 1 : $orden['ORDEN'] + 1;
           
            $pathInS3 = 'test/' . $key_car . '/' . $file_name;
            $link = "http://s3.eu-west-3.amazonaws.com/dealerbest.adoptauncoche.images/" . $pathInS3;
            $res_2 = $Schema->executeQuery('SELECT VEHICULO FROM VEHICULOS_MULTIMEDIA_LINKS WHERE VEHICULO = ? AND ORIGEN = ? AND LINK = ?', 'iss', [$key_car, $id_origin, $link]);
            if($res_2->num_rows == 0) {
                
                $Schema->executeQuery('INSERT INTO VEHICULOS_MULTIMEDIA_LINKS(CODIGO, ORDEN, VEHICULO, TIPO, LINK, FECHA, ORIGEN) VALUE(?, ?, ?, ?, ?, ?, ?)', 'siiisss', [Statics::get_id_unico(), $position, $key_car, 1, $link, $date, $id_origin]);
                $status = 'Successful';
                $message = 'Images uploaded';

            } else {

                $Schema->executeQuery('UPDATE VEHICULOS_MULTIMEDIA_LINKS SET LINK = ?, FECHA = ? WHERE VEHICULO = ? AND ORDEN = ?', 'ssii',[$link, $date, $key_car, $position_file]);
                $status = 'Updated';
                $message = 'Images updated';

            }
            array_push($imagenes, $link);
            $data = [
                'status'            =>      $status,
                'message'           =>      $message,
                'images'            =>      $imagenes
            ];
            $bd_conn->close();
        } else {
            $data = [
                'status'            =>  'Warning',
                'message'           =>  'Car key, file name or origin is empty'
            ];
        }
    } else {
        $data = [
            'status'            =>  'Error',
            'message'           =>  'Car key, file name or origin not found'
        ];
    }
    
}elseif ($auth == 0) {
    $data = [
        "code"      =>  "unauthorized_client",
        "message"   =>  "Invalid authorization token"
    ];
    
}


header("Content-type: application/json; charset=utf-8");
echo json_encode($data);



?>