<?php
declare(strict_types=1);

namespace Tests\Factories;

final class PaymentFactory extends Factory
{
    protected function table(): string
    {
        return 'payments';
    }

    protected function defaults(): array
    {
        return [
            'member_id' => $this->getOrCreateMemberId(),
            'payment_type' => 'annual_subscription',
            'amount' => '527.00',
            'currency' => 'EGP',
            'payment_method' => 'cash',
            'payment_date' => date('Y-m-d'),
            'is_voided' => 0,
            'created_at' => date('Y-m-d H:i:s'),
            'updated_at' => date('Y-m-d H:i:s'),
        ];
    }

    private function getOrCreateMemberId(): int
    {
        $member = $this->db->selectOne(
            "SELECT id FROM members WHERE is_archived = 0 LIMIT 1"
        );
        if ($member) {
            return (int) $member['id'];
        }

        return (new MemberFactory($this->db))->create()['id'];
    }
}
