<?php

namespace Modules\Internal\Services;

use App\Exceptions\ImportRowException;
use App\Models\Benefit;
use App\Models\Corporate;
use App\Models\Drug;
use App\Models\Formularium;
use App\Models\CorporateFormularium;
use App\Models\Plan;
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;

class FormulariumService
{    
    protected function validateFormulariumRow($row)
    {
        if (empty($row['code'])) {
            throw new ImportRowException(__('formularium.CODE_REQUIRED'), 0, null, $row);
        }

        if (empty($row['item_code'])) {
            throw new ImportRowException(__('formularium.TYPE_REQUIRED'), 0, null, $row);
        }
    }

    public function handleFormulariumRow(Formularium $formularium, $row)
    {
        try {
            $formularium_item_data = $row;
            $formularium_item_data["corporate_id"] = $formularium->id;

            $this->validatePlanRow($formularium_item_data);
            $drug = Drug::where('code', $formularium_item_data['item_code'])->first();

            if (empty($drug)) {
              throw new ImportRowException(__('formularium.DRUG_CODE_NOT_FOUND'), 0, null, $row);
            }

            $formulariumItem = $formularium->items()->create([
                'item_id' =>  $drug->id,
            ]);

            return $formulariumItem;
        } catch (\Exception $e) {
            throw $e;
        }
    }

    public function handleFormuariumTemplateRow($row, $id)
    {
        try {
            $formularium = Formularium::updateOrCreate(
                [
                    'code' => $row['code'],
                    'formularium_template_id' => $id
                ],
                [
                    'code' => $row['code'],
                    'name' => $row['name'],
                    'description' => $row['description'],
                    'manufacturer' => $row['manufacturer'],
                    'category_name' => $row['category_name'],
                    'kategori_obat' => $row['kategori_obat'],
                    'uom' => $row['uom'],
                    'composition' => $row['composition'],
                    'general_indication' => $row['general_indication'],
                    'atc_code' => $row['atc_code'],
                    'class' => $row['class'],
                    'bpom_registration' => $row['bpom_registration'],
                    'classifications' => $row['classifications'],
                    'cat_for' => $row['cat_for'],
                    'formularium_template_id' => $id,
                ]);

            return $formularium;
        } catch (\Exception $e) {
            throw $e;
        }
    }

    public function handleFormuariumCorporateRow($row, $id)
    {
        try {
            $formularium = CorporateFormularium::updateOrCreate(
                [
                    'formularium_id' => $row['formularium_id'],
                    'corporate_id' => $id
                ],
                [
                    'formularium_id' => $row['formularium_id'],
                    'corporate_id' => $id,
                    'active' => $row['active']
                ]);

            return $formularium;
        } catch (\Exception $e) {
            throw $e;
        }
    }

    public $listing_doc_headers = [
        "Internal Code",
        "Name",
        "Description",
        "Manufacturer",
        "Category Name",
        "Kategori Obat",
        "UOM",
        "Composition",
        "General Indication",
        "ATC Code",
        "Class",
        "BPOM Registration",
        "Classifications",
        "Cat For (O = obat, VS = Vitamin Suplemen, H=herbal, M=makanan, etc)	",      												 	
    ];


}
