<?php

namespace App\Subscription\Entity;

use App\Subscription\Repository\SubscriptionPeriodRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Uid\Uuid;

#[ORM\Entity(repositoryClass: SubscriptionPeriodRepository::class)]
#[ORM\Table(name: 'subscription_periods')]
class SubscriptionPeriod
{
    #[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: SubscriptionPlan::class, inversedBy: 'periods')]
    #[ORM\JoinColumn(nullable: false, onDelete: 'RESTRICT')]
    private SubscriptionPlan $plan;

    #[ORM\Column(type: 'string', length: 50)]
    private string $label;

    #[ORM\Column(name: 'duration_months', type: 'smallint')]
    private int $durationMonths;

    #[ORM\Column(name: 'price_rials', type: 'integer')]
    private int $priceRials = 0;

    #[ORM\Column(name: 'is_trial', type: 'boolean')]
    private bool $isTrial = false;

    #[ORM\Column(type: 'boolean')]
    private bool $active = true;

    #[ORM\Column(name: 'sort_order', type: 'smallint')]
    private int $sortOrder = 0;

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

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

    public function __construct(SubscriptionPlan $plan, string $label, int $durationMonths, int $priceRials, bool $isTrial = false)
    {
        $this->uuid           = Uuid::v4()->toRfc4122();
        $this->plan           = $plan;
        $this->label          = $label;
        $this->durationMonths = $durationMonths;
        $this->priceRials     = $priceRials;
        $this->isTrial        = $isTrial;
        $this->createdAt      = time();
        $this->updatedAt      = time();
    }

    public function getId(): ?int               { return $this->id; }
    public function getUuid(): string           { return $this->uuid; }
    public function getPlan(): SubscriptionPlan { return $this->plan; }
    public function getLabel(): string          { return $this->label; }
    public function getDurationMonths(): int    { return $this->durationMonths; }
    public function getPriceRials(): int        { return $this->priceRials; }
    public function isTrial(): bool             { return $this->isTrial; }
    public function isActive(): bool            { return $this->active; }
    public function getSortOrder(): int         { return $this->sortOrder; }
    public function getCreatedAt(): int         { return $this->createdAt; }
    public function getUpdatedAt(): int         { return $this->updatedAt; }

    public function setLabel(string $label): self          { $this->label = $label; $this->updatedAt = time(); return $this; }
    public function setDurationMonths(int $v): self        { $this->durationMonths = $v; $this->updatedAt = time(); return $this; }
    public function setPriceRials(int $v): self            { $this->priceRials = $v; $this->updatedAt = time(); return $this; }
    public function setActive(bool $active): self          { $this->active = $active; $this->updatedAt = time(); return $this; }
    public function setSortOrder(int $order): self         { $this->sortOrder = $order; $this->updatedAt = time(); return $this; }

    public function toArray(): array
    {
        return [
            'uuid'           => $this->uuid,
            'plan_uuid'      => $this->plan->getUuid(),
            'label'          => $this->label,
            'duration_months'=> $this->durationMonths,
            'price_rials'    => $this->priceRials,
            'is_trial'       => $this->isTrial,
            'active'         => $this->active,
            'sort_order'     => $this->sortOrder,
        ];
    }
}
