<?php

namespace Daylight\Core\Modules\FormBuilder\Models;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;

class Form extends Model
{
    protected $fillable = [
        'name',
        'recipient_email',
        'cc',
        'bcc',
        'auto_reply_enabled',
        'auto_reply_subject',
        'auto_reply_message',
        'is_active',
        'fields',
        'success_type',
        'success_message',
        'success_redirect_url',
    ];

    protected $attributes = [
        'fields' => '[]',
        'success_type' => 'message',
    ];

    protected function casts(): array
    {
        return [
            'fields' => 'array',
            'is_active' => 'boolean',
            'auto_reply_enabled' => 'boolean',
        ];
    }

    public function submissions(): HasMany
    {
        return $this->hasMany(FormSubmission::class);
    }

    public function scopeActive(Builder $query): Builder
    {
        return $query->where('is_active', true);
    }
}
