<?php

namespace App\Observers;

use App\Models\ActivityRule;
use App\Models\UserCode;
use RuLong\Identity\Models\IdentityCode;

class ActivityRuleObserver
{

    /**
     * Handle the ActivityRule "created" event.
     * @param ActivityRule $rule
     * @return void
     */
    public function created(ActivityRule $rule)
    {
        $info = IdentityCode::where('code', $rule->code)->exists();
        if (!$info) {
            IdentityCode::create([
                'identity_id' => 1,
                'name'        => $rule->title,
                'code'        => $rule->code,
                'profit'      => 0,
            ]);
        }
    }

    /**
     * Handle the ActivityRule "updated" event.
     * @param ActivityRule $rule
     * @return void
     */
    public function updated(ActivityRule $rule)
    {
        $info = IdentityCode::where('code', $rule->code)->exists();
        if (!$info) {
            IdentityCode::create([
                'identity_id' => 1,
                'name'        => $rule->title,
                'code'        => $rule->code,
                'profit'      => 0,
            ]);
        }
    }

    /**
     * Handle the ActivityRule "deleted" event.
     * @param ActivityRule $rule
     * @return void
     */
    public function deleted(ActivityRule $rule)
    {
        IdentityCode::where('code', $rule->code)->delete();
        UserCode::where('code', $rule->code)->delete();
    }

    /**
     * Handle the ActivityRule "restored" event.
     * @param ActivityRule $rule
     * @return void
     */
    public function restored(ActivityRule $rule)
    {
        //
    }

    /**
     * Handle the ActivityRule "force deleted" event.
     * @param ActivityRule $rule
     * @return void
     */
    public function forceDeleted(ActivityRule $rule)
    {
        //
    }

}
