<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class OdooJvStep extends Model
{
    protected $table = 'odoo_jv_steps';
    protected $fillable = [
        'section_id', 
        'step_key', 
        'label', 
        'voucher_type', 
        'is_enabled', 
        'sort_order',
        'partner_id',
        'partner_type'
    ];

    public function section()
    {
        return $this->belongsTo(OdooJvSection::class, 'section_id');
    }

    public function entries()
    {
        return $this->hasMany(OdooJvEntry::class, 'step_id')->orderBy('sequence');
    }

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

        $section = $this->section;
        if (!$section) {
            return null;
        }

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

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