<?php

namespace App\Models;

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

class RecurringInvoice extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'name',
        'customer_id',
        'frequency',
        'start_date',
        'end_date',
        'occurrences',
        'next_invoice_date',
        'status',
        'invoice_data',
        'created_by',
    ];

    protected function casts(): array
    {
        return [
            'start_date' => 'date',
            'end_date' => 'date',
            'next_invoice_date' => 'date',
            'occurrences' => 'integer',
            'invoice_data' => 'array',
        ];
    }

    /**
     * Get the customer that owns the recurring invoice
     */
    public function customer(): BelongsTo
    {
        return $this->belongsTo(Customer::class);
    }

    /**
     * Get the user who created the recurring invoice
     */
    public function creator(): BelongsTo
    {
        return $this->belongsTo(User::class, 'created_by');
    }

    /**
     * Get all invoices generated from this recurring invoice
     */
    public function invoices(): HasMany
    {
        return $this->hasMany(Invoice::class);
    }
}
