<?php

namespace App\Models;

/**
 * Staff-enrolled club membership — one row per guest (like
 * LoyaltyAccountModel, but hand-managed rather than a running total fed
 * by an append-only ledger, since there's no per-event math to keep
 * consistent here).
 */
class DiningMembershipModel extends TenantModel
{
    protected $table            = 'dining_memberships';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'object';
    protected $useTimestamps    = true;

    protected $allowedFields = [
        'guest_id',
        'tier',
        'joined_at',
        'renewal_date',
        'status',
    ];

    public function forGuest(int $guestId): ?object
    {
        return $this->where('guest_id', $guestId)->first();
    }

    /** Batches a list-page lookup into one query, keyed by guest_id — same pattern as LoyaltyAccountModel::forGuests(). */
    public function forGuests(array $guestIds): array
    {
        if (empty($guestIds)) {
            return [];
        }

        $rows = $this->whereIn('guest_id', $guestIds)->where('status', 'active')->findAll();
        $byGuest = [];

        foreach ($rows as $row) {
            $byGuest[$row->guest_id] = $row;
        }

        return $byGuest;
    }
}
