<?php

namespace App\Models\Central;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;

class Subscription extends Model
{
    public const STATUSES = [
        'pending' => 'În așteptare',
        'paid' => 'Plătit',
        'overdue' => 'Depășit termen',
        'cancelled' => 'Anulat',
        'refunded' => 'Refundat',
    ];

    public const PERIODS = [
        'monthly' => 'Lunar',
        'yearly' => 'Anual',
    ];

    public const PAYMENT_METHODS = [
        'card' => 'Card',
        'bank_transfer' => 'Transfer bancar',
        'cash' => 'Numerar',
        'other' => 'Altă metodă',
    ];

    protected $fillable = [
        'company_id', 'plan_id',
        'period', 'amount', 'currency', 'status',
        'period_start', 'period_end', 'paid_at', 'due_at',
        'invoice_number', 'payment_method', 'reference', 'notes',
    ];

    protected $casts = [
        'amount' => 'decimal:2',
        'period_start' => 'date',
        'period_end' => 'date',
        'paid_at' => 'datetime',
        'due_at' => 'datetime',
    ];

    public function company(): BelongsTo
    {
        return $this->belongsTo(Company::class);
    }

    public function plan(): BelongsTo
    {
        return $this->belongsTo(Plan::class);
    }

    public static function generateInvoiceNumber(): string
    {
        return sprintf('INV-%s-%04d',
            date('Ym'),
            (static::whereYear('created_at', date('Y'))
                ->whereMonth('created_at', date('m'))
                ->count()) + 1
        );
    }
}
