<?php
require_once __DIR__ . '/../init.php';
require_once PROJECT_ROOT . '/backend/controllers/common_function.php';
require_once PROJECT_ROOT . '/backend/controllers/db_select_helper.php';

class LoginHistoryController
{
    private $conn;
    private $table = "login_history";
    private $pk = "IDLoginHistory";

    protected $displayedColumn = [
        'IDLoginHistory',
        'UserUsed',
        'DeviceData',
        'IP',
        'GPS',
        'StatusLogin',
        'RemarksLogin',
        'AppUsed',
        'AppVersion',
        'GPS_Lat',
        'GPS_Lng',
        'LastUpdate'
    ];

    public function __construct($conn)
    {
        $this->conn = $conn;
    }

    // SELECT NORMAL
    public function select($post)
    {
        return runSelect($this->conn, $this->table, $this->pk, $this->displayedColumn, $post);
    }

    // INSERT
    public function insert($post, $StatusLogin, $RemarksLogin)
    {
        try {
            $UserUsed = isset($post['Username']) ? $this->conn->real_escape_string($post['Username']) : ''; 
            $IP = isset($post['IP']) ? $this->conn->real_escape_string($post['IP']) : '';
            $GPS = isset($post['GPS']) ? $this->conn->real_escape_string($post['GPS']) : '';
            $StatusLogin = isset($StatusLogin) ? intval($StatusLogin) : 0;
            $RemarksLogin = isset($RemarksLogin) ? $this->conn->real_escape_string($RemarksLogin) : ''; 
            $GPS_Lat = isset($post['GPS_Lat']) ? $this->conn->real_escape_string($post['GPS_Lat']) : '';
            $GPS_Lng = isset($post['GPS_Lng']) ? $this->conn->real_escape_string($post['GPS_Lng']) : '';

            $di = json_decode($post['DeviceInfo'], true);
            $AppName = $di['AppName'] ?? "";
            $AppVersion = $di['AppVersion'] ?? "";
            $Platform = $di['platform'] ?? '';
            $DeviceName = $di['device_name'] ?? '';
            $FullDevice = $di['FullDevice'] ?? $Platform . " - " . $DeviceName;

            $sql = "INSERT INTO {$this->table} 
                (UserUsed, DeviceData, IP, GPS, StatusLogin, RemarksLogin, AppUsed, AppVersion, GPS_Lat, GPS_Lng, LastUpdate) 
                VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NOW())";

            $stmt = $this->conn->prepare($sql);
            if (!$stmt) {
                throw new Exception("Prepare failed: " . $this->conn->error);
            }

            $stmt->bind_param(
                "ssssisssss",
                $UserUsed,
                $FullDevice,
                $IP,
                $GPS,
                $StatusLogin,
                $RemarksLogin,
                $AppName,
                $AppVersion,
                $GPS_Lat,
                $GPS_Lng
            );

            if (!$stmt->execute()) {
                throw new Exception("Execute failed: " . $stmt->error);
            }

            $insertId = $stmt->insert_id;
            $stmt->close();

            return [
                "Status" => 1,
                "Keterangan" => "Insert berhasil.",
                "IDLoginHistory" => $insertId
            ];

        } catch (Exception $e) {
            return [
                "Status" => 0,
                "Keterangan" => $e->getMessage()
            ];
        }
    }
}
