<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class OdooJvSection extends Model
{
    protected $table = 'odoo_jv_sections';
    protected $fillable = [
        'system_id', 
        'name', 
        'sort_order',
        'partner_id',
        'partner_type'
    ];

    public function system()
    {
        return $this->belongsTo(OdooSystem::class, 'system_id');
    }

    public function steps()
    {
        return $this->hasMany(OdooJvStep::class, 'section_id')->orderBy('sort_order');
    }

    /**
     * Get partner information (generic - doesn't use FK relationship)
     * 
     * @return object|null
     */
    public function getPartnerInfo()
    {
        if (!$this->partner_id || !$this->partner_type) {
            return null;
        }

        $system = $this->system;
        if (!$system || !$system->partner_table) {
            return null;
        }

        return \DB::table($system->partner_table)
            ->where('id', $this->partner_id)
            ->first();
    }
}
