<?php

namespace App\Models;

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

class MexcoInvoiceItem extends Model
{
    public const TYPE_PLAN = 'plan';

    public const TYPE_ADDON = 'addon';

    public const TYPE_DISCOUNT = 'discount';

    public const TYPE_CREDIT = 'credit';

    protected $table = 'mexco_invoice_items';

    protected $fillable = [
        'invoice_id',
        'item_type',
        'item_id',
        'description',
        'quantity',
        'unit_price_cents',
        'total_cents',
    ];

    public function invoice(): BelongsTo
    {
        return $this->belongsTo(MexcoInvoice::class, 'invoice_id');
    }

    public function formattedTotal(): string
    {
        $prefix = $this->total_cents < 0 ? '-' : '';

        return $prefix.'MYR '.number_format(abs($this->total_cents) / 100, 2);
    }
}
