<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class DailyProfit extends Model
{
    use HasFactory;

    protected $guarded = ['id'];

    public static function createOrUpdate($data)
    {
        $existingRecord = self::where('invest_money_id', $data['invest_money_id'])
            ->whereDate('created_at', $data['created_at'])
            ->first();

        if ($existingRecord) {
            // If the record exists, update it
            $existingRecord->update([
                'profit_percentage' => $data['profit_percentage'],
                'profitted_ammount' => $data['profitted_ammount'],
            ]);
        } else {
            // If the record doesn't exist, create a new one
            self::create($data);
        }
    }
}
