<?php

namespace App\Models;

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

#[Fillable([
    'subscription_id',
    'provider_key',
    'provider_invoice_id',
    'amount_cents',
    'tax_cents',
    'tax_rate_bps',
    'tax_label',
    'tax_country',
    'currency',
    'status',
    'paid_at',
    'hosted_url',
])]
class Invoice extends Model
{
    protected function casts(): array
    {
        return [
            'paid_at' => 'datetime',
            'amount_cents' => 'integer',
            'tax_cents' => 'integer',
            'tax_rate_bps' => 'integer',
        ];
    }

    /** Amount minus tax — derived getter for UI tooltip + admin breakdown. */
    public function subtotalCents(): int
    {
        return max(0, (int) $this->amount_cents - (int) $this->tax_cents);
    }

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