<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\Eloquent\Relations\HasMany;

class SupplierAccount extends Model
{
    use HasFactory;

    protected $table = 'supplier_accounts';

    protected $fillable = [
        'supplier_code',
        'supplier_account',
        'contact_person',
        'contact_1',
        'contact_2',
        'email',
        'address',
        'category_id',
        'sales_rep_id',
        'age_analysis_id',
        'area_id',
    ];

    /**
     * Update a supplier account.
     *
     * @param int $id
     * @param string $supplier_code
     * @param string $supplier_account
     * @param string|null $contact_person
     * @param string|null $contact_1
     * @param string|null $contact_2
     * @param string|null $email
     * @param string|null $address
     * @param int|null $category_id
     * @param int|null $sales_rep_id
     * @param int|null $age_analysis_id
     * @param int|null $area_id
     * @return int Number of affected rows
     */
    public static function editAccount(
        $id,
        $code,
        $account,
        $person,
        $contact1,
        $contact2,
        $email,
        $address,
        $categoryId,
        $salesRepId,
        $ageAnalysisId,
        $areaId
    ) {
        return DB::update(
            "UPDATE supplier_accounts SET 
                supplier_code = ?, 
                supplier_account = ?, 
                contact_person = ?, 
                contact_1 = ?, 
                contact_2 = ?, 
                email = ?, 
                address = ?, 
                category_id = ?, 
                sales_rep_id = ?, 
                age_analysis_id = ?, 
                area_id = ? 
             WHERE id = ?",
            [
                $code,
                $account,
                $person,
                $contact1,
                $contact2,
                $email,
                $address,
                $categoryId,
                $salesRepId,
                $ageAnalysisId,
                $areaId,
                $id
            ]
        );
    }

    /**
     * Delete a supplier account by ID.
     *
     * @param int $id
     * @return int Number of deleted rows
     */
    public static function deleteAccount($id)
    {
        return DB::delete("DELETE FROM supplier_accounts WHERE id = ?", [$id]);
    }

    /**
     * Get the category that this supplier belongs to.
     */
    public function category()
    {
        return $this->belongsTo(SupplierCategory::class, 'category_id');
    }

    /**
     * Get the sales representative for this supplier.
     */
    public function salesRep()
    {
        return $this->belongsTo(SalesRep::class, 'sales_rep_id');
    }

    /**
     * Get the age analysis for this supplier.
     */
    public function ageAnalysis()
    {
        return $this->belongsTo(SupAgeing::class, 'age_analysis_id');
    }

    /**
     * Get the area for this supplier.
     */
    public function area()
    {
        return $this->belongsTo(Area::class, 'area_id');
    }
    
    /**
     * All SupplierDocumentHeader records for this supplier.
     */
    public function documentHeaders(): HasMany
    {
        return $this->hasMany(SupplierDocumentHeader::class, 'supplier_account_id');
    }

    /**
     * All SupplierJournal records for this supplier.
     */
    public function journals(): HasMany
    {
        return $this->hasMany(SupplierJournal::class, 'supplier_account_id');
    }
}