<?php

namespace App\Settlement\Entity;

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

#[ORM\Entity(repositoryClass: SettlementRepository::class)]
#[ORM\Table(name: 'settlements')]
#[ORM\Index(columns: ['user_id', 'status'], name: 'idx_settlements_user_status')]
class Settlement
{
    public const STATUS_PENDING  = 'pending';
    public const STATUS_APPROVED = 'approved';
    public const STATUS_REJECTED = 'rejected';
    public const STATUS_PAID     = 'paid';

    #[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: User::class)]
    #[ORM\JoinColumn(name: 'user_id', referencedColumnName: 'id', nullable: false, onDelete: 'RESTRICT')]
    private User $user;

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

    #[ORM\Column(type: 'string', length: 20)]
    private string $status = self::STATUS_PENDING;

    #[ORM\Column(name: 'bank_account', type: 'json', nullable: true)]
    private ?array $bankAccount = null;

    #[ORM\Column(name: 'admin_note', type: 'string', length: 500, nullable: true)]
    private ?string $adminNote = null;

    #[ORM\Column(name: 'receipt', type: 'string', length: 500, nullable: true)]
    private ?string $receipt = null;

    #[ORM\Column(name: 'reviewed_by', type: 'integer', nullable: true)]
    private ?int $reviewedBy = null;

    #[ORM\Column(name: 'reviewed_at', type: 'integer', nullable: true)]
    private ?int $reviewedAt = null;

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

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

    public function __construct(User $user, int $amountRials, ?array $bankAccount = null)
    {
        $this->uuid        = Uuid::v4()->toRfc4122();
        $this->user        = $user;
        $this->amountRials = $amountRials;
        $this->bankAccount = $bankAccount;
        $this->createdAt   = time();
        $this->updatedAt   = time();
    }

    public function getId(): ?int            { return $this->id; }
    public function getUuid(): string        { return $this->uuid; }
    public function getUser(): User          { return $this->user; }
    public function getAmountRials(): int    { return $this->amountRials; }
    public function getStatus(): string      { return $this->status; }
    public function getBankAccount(): ?array { return $this->bankAccount; }
    public function getAdminNote(): ?string  { return $this->adminNote; }
    public function getReceipt(): ?string    { return $this->receipt; }

    public function setReceipt(?string $receipt): self
    {
        $this->receipt   = $receipt;
        $this->updatedAt = time();
        return $this;
    }

    public function approve(int $adminUserId, ?string $note = null): self
    {
        $this->status     = self::STATUS_APPROVED;
        $this->reviewedBy = $adminUserId;
        $this->reviewedAt = time();
        $this->adminNote  = $note;
        $this->updatedAt  = time();
        return $this;
    }

    public function reject(int $adminUserId, string $note): self
    {
        $this->status     = self::STATUS_REJECTED;
        $this->reviewedBy = $adminUserId;
        $this->reviewedAt = time();
        $this->adminNote  = $note;
        $this->updatedAt  = time();
        return $this;
    }

    public function markPaid(?string $receipt = null): self
    {
        $this->status    = self::STATUS_PAID;
        if ($receipt !== null) $this->receipt = $receipt;
        $this->updatedAt = time();
        return $this;
    }

    public function toArray(): array
    {
        return [
            'uuid'         => $this->uuid,
            'amount_rials' => $this->amountRials,
            'status'       => $this->status,
            'bank_account' => $this->bankAccount,
            'admin_note'   => $this->adminNote,
            'receipt'      => $this->receipt,
            'reviewed_at'  => $this->reviewedAt,
            'created_at'   => $this->createdAt,
        ];
    }
}
