<?php

namespace classes;

class Location
{
    private array $locationDetails;
    private string $locationCode;
    private MysqliVeu $database;


    public function __construct(?MysqliVeu $database = null)
    {
        if (!$database)
        {
            $database = new MysqliVeu();
        }

        $this->database = $database;
    }

    /**
     * Retrieves location record details based on the provided location code.
     *
     * @param string $locationCode The location code used to fetch the corresponding location record.
     * @return array The details of the location record, including information from the location type.
     */
    public function getLocationRecordByLocationCode(string $locationCode): array
    {
        $this->locationCode = trim($locationCode);
        $sql = "SELECT
                location.*,
                location_type.no_serial_scan
            FROM location
                JOIN location_type ON location.location_type_id = location_type.id
            WHERE LOWER(location_code) = ?";
        return $this->locationDetails = $this->database->queryParamsRow($sql, [$this->locationCode]);
    }

    /**
     * Retrieves a location record from the database using the provided location ID.
     *
     * @param int $locationId The unique identifier of the location to retrieve.
     * @return array An associative array containing the location record details.
     */
    public function getLocationRecordByLocationId(int $locationId): array
    {
        $sql = "SELECT * FROM location WHERE id = ?";
        return $this->locationDetails = $this->database->queryParamsRow($sql, [$locationId]);
    }

    /**
     * Retrieves the ID of a location type based on the provided location type code.
     *
     * @param string         $locationTypeCode The unique code of the location type to retrieve the ID for.
     * @param MysqliVeu|null $database An optional database connection; if not provided, a new connection is created.
     * @return int The unique identifier of the location type.
     */
    public static function getLocationTypeId(string $locationTypeCode, MysqliVeu $database = null): int
    {
        // Checks if a database connection was provided
        if (!$database)
        {
            $database = new MysqliVeu();
        }

        $locationTypeCode = strtoupper($locationTypeCode);

        $sql = "SELECT id
            FROM location_type
            WHERE location_type_code = ?";

        $result = $database->queryParamsRow($sql, [$locationTypeCode]);

        return (int) $result['id'];
    }

    /**
     * Retrieves the ID corresponding to a specified location subtype code.
     *
     * @param string         $locationSubtypeCode The unique code representing the location subtype.
     * @param MysqliVeu|null $database An optional database connection instance. If null, a new instance will be created.
     * @return int The ID of the location subtype associated with the provided code.
     */
    public static function getLocationSubtypeId(string $locationSubtypeCode, MysqliVeu $database = null): int
    {
        // Checks if a database connection was provided
        if (!$database)
        {
            $database = new MysqliVeu();
        }

        $locationSubtypeCode = strtoupper($locationSubtypeCode);

        $sql = 'SELECT id
            FROM location_sub_type
            WHERE location_sub_type_code = ?';

        $result = $database->queryParamsRow($sql, [$locationSubtypeCode]);

        return (int) $result['id'];
    }

    /**
     * Retrieves the location details.
     *
     * @return array Returns an array of location details.
     */
    public function getLocationDetails(): array
    {
        return $this->locationDetails;
    }

    /**
     * Validates the given location hash code against the stored location hash.
     *
     * @param string $locationHashCode The location hash code to validate.
     * @return bool Returns true if the given location hash code matches the stored location hash, otherwise false.
     */
    public function validateLocationHash(string $locationHashCode): bool
    {
        $locationHashCode = trim($locationHashCode);

        return $locationHashCode === $this->locationDetails['location_hash'];
    }
}
