<?php

declare(strict_types=1);

namespace App\Domain\AppInstaller\Models;

use App\Domain\Account\Models\Account;
use App\Domain\Database\Models\MysqlDatabase;
use App\Domain\Domain\Models\Domain;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\SoftDeletes;

/**
 * @property int $id
 * @property int $account_id
 * @property int|null $domain_id
 * @property string $app_type
 * @property string $version
 * @property string $install_url
 * @property string $install_path
 * @property int|null $database_id
 * @property string|null $admin_username
 * @property string|null $admin_email
 * @property string|null $site_title
 * @property string $language
 * @property string $status
 * @property bool $updates_available
 * @property string|null $latest_available_version
 */
class InstalledApp extends Model
{
    use SoftDeletes;

    protected $fillable = [
        'account_id', 'domain_id', 'app_type', 'version', 'install_url', 'install_path',
        'database_id', 'admin_username', 'admin_email', 'admin_password_hash',
        'site_title', 'language', 'config_overrides',
        'status', 'last_error', 'installed_at', 'last_checked_at',
        'updates_available', 'latest_available_version',
        // WordPress Toolkit Phase 2
        'wp_version', 'wp_debug_enabled', 'wp_maintenance_mode',
        'auto_update_core', 'auto_update_plugins', 'auto_update_themes',
        'security_hardened', 'is_staging_of', 'staging_label',
    ];

    protected function casts(): array
    {
        return [
            'config_overrides' => 'array',
            'updates_available' => 'boolean',
            'installed_at' => 'datetime',
            'last_checked_at' => 'datetime',
            // WordPress Toolkit Phase 2
            'wp_debug_enabled' => 'boolean',
            'wp_maintenance_mode' => 'boolean',
            'auto_update_plugins' => 'boolean',
            'auto_update_themes' => 'boolean',
            'security_hardened' => 'boolean',
        ];
    }

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

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

    public function database(): BelongsTo
    {
        return $this->belongsTo(MysqlDatabase::class, 'database_id');
    }
}
