<?php
namespace Components\Models;

class Customer extends BaseModel
{
    protected static string $table = 'customers';
    protected static string $primaryKey = 'customers_id';

    public static function isReadOnly(): bool
    {
        return true;
    }

    public function info(): ?CustomerInfo
    {
        $rows = $this->hasMany(CustomerInfo::class, 'customers_info_id', static::pk());
        return $rows[0] ?? null;
    }

    /**
     * @return array<int, AddressBook>
     */
    public function addresses(): array
    {
        return $this->hasMany(AddressBook::class, 'customers_id', static::pk());
    }

    public function defaultAddress(): ?AddressBook
    {
        $address = $this->belongsTo(AddressBook::class, 'customers_default_address_id', 'address_book_id');
        return $address instanceof AddressBook ? $address : null;
    }

    /**
     * @return array<int, CustomerBasket>
     */
    public function baskets(): array
    {
        return $this->hasMany(CustomerBasket::class, 'customers_id', static::pk());
    }

    /**
     * @return array<int, CustomerBasketAttribute>
     */
    public function basketAttributes(): array
    {
        return $this->hasMany(CustomerBasketAttribute::class, 'customers_id', static::pk());
    }
}
