<?php

namespace App\Models\Master;

use CodeIgniter\Model;

class HargaMarketplaceModel extends Model
{
    protected $table      = 'tb_harga_marketplace';
    protected $primaryKey = 'id_harga_marketplace';

    protected $returnType = 'array';

    protected $allowedFields = [
        'id_marketplace',
        'id_produk',
        'harga_marketplace',
        'komisi_produk',
    ];

    protected $useTimestamps = false;

    /**
     * Ambil semua harga produk + nama produk untuk satu marketplace.
     * Dipakai di controller index() dan untuk editData() di JS.
     */
    public function getByMarketplace(int $id_marketplace): array
    {
        return $this->db->table('tb_harga_marketplace hm')
            ->select('hm.id_produk, hm.harga_marketplace, hm.komisi_produk, p.nama_produk')
            ->join('tb_produk p', 'p.id_produk = hm.id_produk', 'left')
            ->where('hm.id_marketplace', $id_marketplace)
            ->get()
            ->getResultArray();
    }
}