<?php

namespace App\Secretary\Entity;

use App\Auth\Entity\User;
use App\Secretary\Repository\SecretaryEarningRepository;
use App\Settlement\Entity\FinancialBreakdown;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;

/**
 * سهم یک منشی از یک نوبت آنلاین.
 *
 * جدول جداست (نه JSON روی FinancialBreakdown) چون گزارش پنل منشی باید per-user
 * فیلتر و جمع‌بندی شود؛ یک پرداخت می‌تواند چند منشیِ سهم‌بر داشته باشد.
 */
#[ORM\Entity(repositoryClass: SecretaryEarningRepository::class)]
#[ORM\Table(name: 'secretary_earnings')]
#[ORM\Index(columns: ['secretary_user_id', 'created_at'], name: 'idx_secretary_earnings_user_time')]
class SecretaryEarning
{
    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private ?int $id = null;

    #[ORM\Column(type: 'string', length: 36, unique: true)]
    private string $uuid;

    #[ORM\ManyToOne(targetEntity: FinancialBreakdown::class)]
    #[ORM\JoinColumn(name: 'breakdown_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
    private FinancialBreakdown $breakdown;

    #[ORM\ManyToOne(targetEntity: User::class)]
    #[ORM\JoinColumn(name: 'secretary_user_id', referencedColumnName: 'id', nullable: false, onDelete: 'CASCADE')]
    private User $secretary;

    /** رابطهٔ منشی–پزشک/کلینیکی که سهم از آن آمده (snapshot، برای ردگیری). */
    #[ORM\Column(name: 'relation_uuid', type: 'string', length: 36)]
    private string $relationUuid;

    #[ORM\Column(name: 'share_percent', type: 'decimal', precision: 5, scale: 2)]
    private string $sharePercent;

    #[ORM\Column(name: 'share_rials', type: 'integer')]
    private int $shareRials;

    #[ORM\Column(name: 'created_at', type: 'integer')]
    private int $createdAt;

    public function __construct(
        FinancialBreakdown $breakdown,
        User $secretary,
        string $relationUuid,
        float $sharePercent,
        int $shareRials,
    ) {
        $this->uuid         = Uuid::v4()->toRfc4122();
        $this->breakdown    = $breakdown;
        $this->secretary    = $secretary;
        $this->relationUuid = $relationUuid;
        $this->sharePercent = number_format($sharePercent, 2, '.', '');
        $this->shareRials   = $shareRials;
        $this->createdAt    = time();
    }

    public function getId(): ?int                        { return $this->id; }
    public function getUuid(): string                    { return $this->uuid; }
    public function getBreakdown(): FinancialBreakdown   { return $this->breakdown; }
    public function getSecretary(): User                 { return $this->secretary; }
    public function getRelationUuid(): string            { return $this->relationUuid; }
    public function getSharePercent(): float             { return (float) $this->sharePercent; }
    public function getShareRials(): int                 { return $this->shareRials; }
    public function getCreatedAt(): int                  { return $this->createdAt; }

    public function toArray(): array
    {
        return [
            'uuid'          => $this->uuid,
            'relation_uuid' => $this->relationUuid,
            'share_percent' => (float) $this->sharePercent,
            'share_rials'   => $this->shareRials,
            'created_at'    => $this->createdAt,
        ];
    }
}
