<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class School extends Model
{
    protected $connection = 'central';

    protected $fillable = [
        'name',
        'database_name',
        'database_username',
        'database_password',
    ];

    protected $hidden = [
        'database_password',
    ];

    public static function getDatabaseConfig(string $name): ?array
    {
        $school = DB::connection('central')->table('schools')->where('name', $name)->first();

        if (!$school) {
            return null;
        }

        return [
            'database' => $school->database_name,
            'username' => $school->database_username ?? env('DB_USERNAME'),
            'password' => $school->database_password ?? env('DB_PASSWORD'),
        ];
    }
}