<?php
/**
 * Member factory for creating member instances.
 *
 * @package Awesomemotive\Rewardswp\Factories
 * @since 1.0.0
 */

namespace Awesomemotive\Rewardswp\Factories;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

use Awesomemotive\Rewardswp\Models\Member;
use DI\Container;
use Exception;

/**
 * Factory class for creating Member instances.
 *
 * @since 1.0.0 // Or your specific version
 */
class MemberFactory {

	/**
	 * The DI container.
	 *
	 * @since 1.0.0
	 * @var Container
	 */
	private Container $container;

	/**
	 * MemberFactory constructor.
	 *
	 * @since 1.0.0
	 * @param Container $container The DI container.
	 */
	public function __construct( Container $container ) {
		$this->container = $container;
	}

	/**
	 * Creates a new Member instance, optionally loading it by ID.
	 *
	 * Uses the DI container's make() method to ensure a new instance is created.
	 * If a member ID is provided, the instance will be populated with data
	 * from the database using its load() method.
	 *
	 * @since 1.0.0 // Or your specific version
	 * @param int|null $member_id Optional. The ID of the member to load.
	 *
	 * @return Member|null A new Member instance, possibly loaded with data.
	 * @throws Exception If the DI container fails to create the Member instance.
	 */
	public function make( ?int $member_id ): ?Member {
		try {
			$member = $this->container->make( Member::class );
			if ( null !== $member_id ) {
				$member->load( $member_id );
			}
		} catch ( Exception $e ) {
			return null;
		}
		return $member;
	}
}
