<?php

namespace App\Models;

use App\Models\SubAccount;
use App\Models\AccountType;
use App\Models\MainSubAccount;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Facades\DB;

class GeneralLedger extends Model
{
    use HasFactory;
    protected $table = "main_accounts";
    protected $fillable = ['code','account_type','financial_category','account_code','account_name'];

    public function accountType()
    {
        return $this->belongsTo(AccountType::class, 'account_type', 'code');
    }


    public function mainSubAccounts()
    {
        return $this->hasMany(MainSubAccount::class, 'parent_id');
    }

    public static function editAccountTypes($id, $type, $AccCode, $AccName)
    {
        return DB::update("UPDATE `main_accounts` SET  `account_type` = ?, `code` = ?, `account_name` = ? WHERE `id` = ?", [ $type, $AccCode, $AccName, $id]);
    }

    public static function deleteAccountTypes($id)
    {
        return DB::delete("DELETE FROM `account_types`  WHERE `id` = ?", [$id]);
    }

    public static function editMainAccount($id, $type, $code, $financial_category, $account_name)
    {
        return DB::update(
            "UPDATE main_accounts SET
                account_type = ?,
                code = ?,
                financial_category = ?,
                account_name = ?
            WHERE id = ?",
            [$type, $code, $financial_category, $account_name, $id]
        );
    }

    public static function deleteMainAccount($id)
    {
        return DB::delete("DELETE FROM main_accounts WHERE id = ?", [$id]);
    }

    // Main Sub Account methods
    public static function editMainSubAccount($id, $account_code, $sub_name)
    {
        return DB::update(
            "UPDATE main_sub_accounts SET account_code = ?, sub_name = ? WHERE id = ?",
            [$account_code, $sub_name, $id]
        );
    }

    public static function deleteMainSubAccount($id)
    {
        return DB::delete("DELETE FROM `main_sub_accounts` WHERE `id` = ?", [$id]);
    }

    // Sub Account 1 methods
    public static function editSubAccount1($id, $account_code, $sub_name)
    {
        return DB::update(
            "UPDATE sub_accounts_1 SET account_code = ?, sub_name = ? WHERE id = ?",
            [$account_code, $sub_name, $id]
        );
    }

    public static function deleteSubAccount1($id)
    {
        return DB::delete("DELETE FROM `sub_accounts_1` WHERE `id` = ?", [$id]);
    }

    // Sub Account 2 methods
    public static function editSubAccount2($id, $account_code, $sub_name)
    {
        return DB::update(
            "UPDATE sub_accounts_2 SET account_code = ?, sub_name = ? WHERE id = ?",
            [$account_code, $sub_name, $id]
        );
    }

    public static function deleteSubAccount2($id)
    {
        return DB::delete("DELETE FROM `sub_accounts_2` WHERE `id` = ?", [$id]);
    }

    // Sub Account 3 methods
    public static function editSubAccount3($id, $account_code, $sub_name)
    {
        return DB::update(
            "UPDATE sub_accounts_3 SET account_code = ?, sub_name = ? WHERE id = ?",
            [$account_code, $sub_name, $id]
        );
    }

    public static function deleteSubAccount3($id)
    {
        return DB::delete("DELETE FROM `sub_accounts_3` WHERE `id` = ?", [$id]);
    }
}
