<?php

namespace App\Models;

/** See CreateBookingGroupsTable's migration comment. */
class BookingGroupModel extends TenantModel
{
    protected $table            = 'booking_groups';
    protected $primaryKey       = 'id';
    protected $useAutoIncrement = true;
    protected $returnType       = 'object';
    protected $useTimestamps    = true;

    protected $allowedFields = [
        'group_name',
        'corporate_account_id',
        'organizer_name',
        'organizer_phone',
        'organizer_email',
        'check_in_date',
        'check_out_date',
        'status',
        'notes',
        'created_by',
    ];

    protected $validationRules = [
        'group_name'     => 'required|max_length[150]',
        'check_in_date'  => 'required|valid_date[Y-m-d]',
        'check_out_date' => 'required|valid_date[Y-m-d]|checkOutAfterCheckIn[check_in_date]',
        'organizer_email' => 'permit_empty|valid_email',
        'status'         => 'permit_empty|in_list[confirmed,cancelled]',
    ];

    /** With the linked corporate account's name joined in, for the list/detail pages. */
    public function withCompany()
    {
        return $this->select('booking_groups.*, corporate_accounts.company_name as company_name')
            ->join('corporate_accounts', 'corporate_accounts.id = booking_groups.corporate_account_id', 'left');
    }

    public function list(): array
    {
        return $this->withCompany()->orderBy('booking_groups.check_in_date', 'desc')->findAll();
    }
}
