<?php

namespace App\Services;

use App\Models\Scaler;
use App\Models\Market;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\DB;

class ScalerService
{
    public function create(array $data): Scaler
    {
        return DB::transaction(function () use ($data) {
            return Scaler::create($data);
        });
    }

    public function approve(Scaler $scaler): bool
    {
        return DB::transaction(function () use ($scaler) {
            $scaler->status = 'active';
            $scaler->approved_at = now();
            $scaler->save();
            return true;
        });
    }

    public function assignMarket(Scaler $scaler, Market $market): bool
    {
        return DB::transaction(function () use ($scaler, $market) {
            $scaler->markets()->attach($market->id);
            return true;
        });
    }

    public function removeMarket(Scaler $scaler, Market $market): bool
    {
        return DB::transaction(function () use ($scaler, $market) {
            $scaler->markets()->detach($market->id);
            return true;
        });
    }
}
