<?php

namespace App\Observers;

use App\Models\Brand;

class BrandObServer
{
    /**
     * Handle the Partner "created" event.
     */
    public function created(Brand $partner): void
    {
        // Update positions of other partners if necessary
        $this->updatePosition($partner, $partner->position);
    }

    /**
     * Handle the Partner "updated" event.
     */
    public function updated(Brand $partner): void
    {
        if ($partner->isDirty("position")) {
            $old = $partner->getOriginal("position");
            $new = $partner->position;
            $this->updatePosition($partner, $new, $old);
        }
    }

    /**
     * Update the position of other partners based on the new and old position.
     */
    public function updatePosition(Brand $partner, $new, $old = null)
    {
        // If old position exists and is greater than or equal to 1, decrement positions greater than the old position
        if ($old !== null && $old >= 1) {
            Brand::query()
                ->where("position", ">", $old) // Partners with position greater than the old one
                ->whereNot("id", $partner->id) // Exclude the current partner
                ->decrement("position"); // Decrement their position
        }

        // Now, increment positions greater than or equal to the new position
        Brand::query()
            ->where("position", ">=", $new) // Partners with position greater than or equal to the new one
            ->whereNot("id", $partner->id) // Exclude the current partner
            ->increment("position"); // Increment their position
    }

    /**
     * Handle the Partner "deleted" event.
     */
    public function deleted(Brand $partner): void
    {
        // If a partner is deleted, their position may need to be adjusted for the remaining partners
        $this->adjustPositionsAfterDelete($partner);
    }

    /**
     * Handle the Partner "restored" event.
     */
    public function restored(Brand $partner): void
    {
        // When restoring a partner, the positions might need to be adjusted as well
        $this->adjustPositionsAfterRestore($partner);
    }

    /**
     * Handle the Partner "force deleted" event.
     */
    public function forceDeleted(Brand $partner): void
    {
        // Handle force delete if needed
    }

    /**
     * Adjust positions of other partners after a partner is deleted.
     */
    private function adjustPositionsAfterDelete(Brand $partner): void
    {
        // Decrement positions of partners that had a greater position
        if ($partner->position != 0) {
            Brand::query()
                ->where("position", ">", $partner->position) // Partners with position greater than the deleted partner's position
                ->decrement("position"); // Decrement their position
        }
    }

    /**
     * Adjust positions after a partner is restored.
     */
    private function adjustPositionsAfterRestore(Brand $partner): void
    {
        // Increment positions of partners that had a greater position
        Brand::query()
            ->where("position", ">=", $partner->position) // Partners with position greater or equal to the restored partner's position
            ->increment("position"); // Increment their position
    }
}
