<?php

defined('BASEPATH') or exit('No direct script access allowed');
require_once(APPPATH . 'libraries/import/App_import.php');

class Import_properties extends App_import
{
    protected $notImportableFields = [
        'id',
        'plan_image_path',
        'embeded_video',
        'area',
        'sections_to_hide_lp',
        'property_status',
        'rel_id',
        'rel_type',
        'total_views',
        'total_leads',
        'created_at'
    ];

    protected $requiredFields = [
        'name',
        'category',
        'selected_agent'
    ];

    public function __construct()
    {
        $this->addItemsGuidelines();
        parent::__construct();
    }

    public function perform()
    {
        $this->initialize();

        $databaseFields      = $this->getImportableDatabaseFields();
        $totalDatabaseFields = count($databaseFields);

        foreach ($this->getRows() as $rowNumber => $row) {
            $insert = [];
            $duplicate = false;
            for ($i = 0; $i < $totalDatabaseFields; $i++) {
                $row[$i] = $this->checkNullValueAddedByUser($row[$i]);
                $insert[$databaseFields[$i]] = $row[$i];
            }

            $insert = $this->trimInsertValues($insert);

            if (count($insert) > 0) {
                $this->incrementImported();
                $id = null;

                if (!$this->isSimulation()) {

                    if (!isset($insert['created_at'])) {
                        $insert['created_at'] = date('Y-m-d H:i:s');
                    }

                    $this->ci->db->insert(db_prefix().'propertybase_properties', $insert);
                    $id = $this->ci->db->insert_id();
                } else {
                    $this->simulationData[$rowNumber] = $this->formatValuesForSimulation($insert);
                }

                $this->handleCustomFieldsInsert($id, $row, $i, $rowNumber, 'propertybase_pr');
            }

            if ($this->isSimulation() && $rowNumber >= $this->maxSimulationRows) {
                break;
            }
        }
    }

    public function formatFieldNameForHeading($field)
    {
        return parent::formatFieldNameForHeading($field);
    }

    protected function failureRedirectURL()
    {
        return admin_url('propertybase/import_properties');
    }

    private function addItemsGuidelines()
    {
        $this->addImportGuidelinesInfo('Fields marked with * are required to enter..', true);
        $this->addImportGuidelinesInfo('For columns like "Country, Energy efficient, Ownership, Energy effiency, Category, Selected agent, " please look up the ID in the settings and fill it in (For Property Agent you will need to put staff id).');
        $this->addImportGuidelinesInfo('Column Property purpose can have values | 1 - Which is Property For Sale | 2 - Which is Property For Rent | 3 - Which is Property For Sale & Rent');
    }

    private function formatValuesForSimulation($values)
    {
        return $values;
    }
}