<?php

namespace App\Campaigns\Publishing;

use App\Campaigns\Publisher;
use App\Models\Campaign;

/**
 * How one platform turns a campaign we hold into a campaign it holds.
 *
 * {@see Publisher} owns everything that is true whatever the
 * platform: the recorded decision, the human approval, the re-check against the
 * gate, the refusal to publish twice, the status transitions and the rollback
 * on failure. None of that is allowed to live in here, because the moment it
 * does a second platform inherits none of it.
 *
 * What is left is genuinely platform work: the shape of the payload, the order
 * the objects have to be created in, and how to remove them again.
 *
 * Everything created must be created paused. That is not this interface's
 * choice to make: nothing this product builds is allowed to start spending on
 * creation, and a platform that cannot create paused cannot be published to.
 */
interface PlatformPublisher
{
    /**
     * Create the campaign and everything under it, paused.
     *
     * The returned array is both the audit record and the rollback instruction,
     * so it must be filled in as objects are created rather than at the end:
     * a failure on the third call still has to know about the first two.
     *
     * `campaign_id` is the one key the caller reads by name, because it is what
     * marks the campaign as published and what stops it being published again.
     *
     * @param  array<string,mixed>  $created  filled in as work proceeds
     * @return array<string,mixed> ids of everything created, including campaign_id
     */
    public function create(Campaign $campaign, string $actor, array &$created): array;

    /**
     * Undo whatever create() got through, deepest first.
     *
     * Called with a partial result after a failure. Must not throw: a rollback
     * that fails has to leave the original error as the thing the user is told
     * about, not replace it with a second one.
     *
     * Returns the platform ids it could not remove, which is not the same as
     * throwing and not the same as success. On 5 Oct a production publish failed
     * at the ad set, the rollback's delete of the campaign came back "(#10)
     * Application does not have permission for this action" because an
     * MCP-created draft is not reachable over Graph, and the buyer was told
     * "anything created during the attempt was removed". It had not been. A
     * caller cannot say what happened unless this says what happened.
     *
     * @param  array<string,mixed>  $created
     * @return list<string> ids left on the platform; empty means nothing remains
     */
    public function rollBack(Campaign $campaign, array $created, string $actor): array;

    /**
     * Send approved changes to the live campaign.
     *
     * CampaignUpdater used to do this itself, against Meta, for every platform:
     * it read the diff, grouped the fields by Meta's entity types and called the
     * Graph API. A Google campaign reaching it would have sent Google ids and a
     * Google customer id to Meta, and then recorded the change as applied,
     * because the snapshot moves on success and nothing checked which platform
     * had been written to.
     *
     * Which fields can arrive here is decided by the platform's own
     * editableAfterPublish(), so an implementation must handle everything its
     * rules claim: a field declared editable and silently not sent is worse than
     * one refused outright, because the campaign then disagrees with the
     * platform and our snapshot says it does not.
     *
     * @param  array<string, array{from: mixed, to: mixed}>  $changes  field to before and after
     */
    public function update(Campaign $campaign, array $changes, string $actor): void;

    /**
     * Start the campaign spending.
     *
     * The whole tree, not the campaign alone. Every platform here has at least
     * three levels and every one of them has its own status: a live campaign over
     * a paused ad set delivers nothing, and looks exactly like a campaign that is
     * live and simply not getting impressions. That is the most expensive kind of
     * silence this product can produce, because the numbers all read as plausible
     * and there is nothing to notice.
     *
     * Called only by {@see Publisher::activate()}, which owns the approval, the
     * audit row and the refusal to start something that is not published or is
     * running already. None of that belongs in here.
     *
     * Must be safe to call twice. A platform that has already been asked for
     * ACTIVE should accept being asked again rather than fail, because a retry
     * after a partial failure is the case this is most likely to be used in.
     */
    public function activate(Campaign $campaign, string $actor): void;

    /**
     * Stop it spending, the same way round.
     *
     * The counterpart of activate(), and the reason activation is safe to offer:
     * a product that can start a campaign and not stop it has handed the buyer a
     * one-way door and sent them to the platform's own UI to close it.
     *
     * The campaign level alone is enough to halt delivery, and it is the level
     * that is paused so that activate() has something to turn back on without
     * having to remember which of the lower objects were live.
     */
    public function pause(Campaign $campaign, string $actor): void;

    /**
     * Whether the platform says this campaign is live, right now.
     *
     * Asked rather than remembered. `activated_at` only records that *this*
     * product started a campaign, and it is written in exactly one place, so a
     * campaign brought under management by CampaignImporter has it null however
     * live it is. Anything deciding "is it already running" from local state
     * alone is therefore wrong for every adopted campaign, which is the whole
     * point of adopting one.
     *
     * Null means the platform could not be asked - no reader for it, no id to
     * read, or the read failed. Null is not "no": a caller that treats it as
     * false will offer to start a campaign that may already be spending, so it
     * has to fall back to local state and say which it is relying on.
     *
     * Only ever called on a path a person initiated, because it costs a read
     * against the hourly budget. It must not be called from anything that
     * renders per turn.
     */
    public function isLive(Campaign $campaign, string $actor): ?bool;
}
