<?php declare(strict_types=1);
/**
 * This file is part of the SplendidBear Websites' projects.
 *
 * Copyright (c) 2026 @ www.splendidbear.org
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace App\Entity;

use App\Repository\UserStatsRepository;
use DateTime;
use Doctrine\DBAL\Types\Types;
use Doctrine\ORM\Mapping\Column;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Id;
use Doctrine\ORM\Mapping\Table;

/**
 * Class UserStats
 *
 * Read-only entity mapped to the user_stats materialized view.
 *
 * @package  App\Entity
 * @author   Lang <https://www.splendidbear.org>
 * @category Class
 * @license  https://www.gnu.org/licenses/lgpl-3.0.en.html GNU Lesser General Public License
 * @link     www.splendidbear.org
 * @since    2026. 04. 20.
 */
#[Entity(repositoryClass: UserStatsRepository::class, readOnly: true)]
class UserStats
{
    #[Id, Column(name: 'user_id')]
    public int $userId = 0;

    #[Column]
    public int $totalGames = 0;

    #[Column]
    public int $wins = 0;

    #[Column]
    public int $losses = 0;

    #[Column]
    public int $draws = 0;

    #[Column]
    public int $totalMines = 0;

    #[Column(type: Types::DECIMAL, precision: 10, scale: 1)]
    public string $totalBonusPoints = '0.0';

    #[Column(type: Types::DECIMAL, precision: 10, scale: 1)]
    public string $avgBonus = '0.0';

    #[Column]
    public int $bestChain = 0;

    #[Column]
    public int $blindHits = 0;

    #[Column]
    public int $edgeMines = 0;

    #[Column]
    public int $gamesWithScores = 0;

    #[Column(type: Types::DATETIME_MUTABLE, nullable: true)]
    public ?DateTime $lastGameAt = null;


    public function getWinRate(): int
    {
        return $this->gamesWithScores > 0
            ? (int)round($this->wins / $this->gamesWithScores * 100)
            : 0;
    }

    public function getAvgScore(): int
    {
        return $this->gamesWithScores > 0
            ? (int)round($this->totalMines / $this->gamesWithScores)
            : 0;
    }
}
