<?php

namespace App\Http\Transformers;

use App\Helpers\Helper;
use App\Models\Accessory;
use App\Models\AccessoryCheckout;
use App\Models\Asset;
use App\Models\Component;
use App\Models\License;
use App\Models\LicenseSeat;
use App\Models\Setting;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Support\Facades\Gate;
use Illuminate\Support\Facades\Storage;

class AssetsTransformer
{
    public function transformAssets(Collection $assets, $total)
    {
        $array = [];
        foreach ($assets as $asset) {
            $array[] = self::transformAsset($asset);
        }

        return (new DatatablesTransformer)->transformDatatables($array, $total);
    }

    public function transformAsset(Asset $asset)
    {
        // This uses the getSettings() method so we're pulling from the cache versus querying the settings on single asset
        $setting = Setting::getSettings();

        $array = [
            'id' => (int) $asset->id,
            'name' => e($asset->name),
            'asset_tag' => e($asset->asset_tag),
            'serial' => e($asset->serial),
            'model' => ($asset->model) ? [
                'id' => (int) $asset->model->id,
                'name' => e($asset->model->name),
            ] : null,
            'byod' => ($asset->byod ? true : false),
            'requestable' => ($asset->requestable ? true : false),
            'model_number' => (($asset->model) && ($asset->model->model_number)) ? e($asset->model->model_number) : null,
            'eol' => (($asset->asset_eol_date != '') && ($asset->purchase_date != '')) ? (int) Carbon::parse($asset->asset_eol_date)->diffInMonths($asset->purchase_date, true).' months' : null,
            'asset_eol_date' => ($asset->asset_eol_date != '') ? Helper::getFormattedDateObject($asset->asset_eol_date, 'date') : null,
            'status_label' => ($asset->status) ? [
                'id' => (int) $asset->status->id,
                'name' => e($asset->status->name),
                'status_type' => e($asset->status->getStatuslabelType()),
                'status_meta' => e($asset->present()->statusMeta),
            ] : null, // <-- legacy - will be removed
            'status' => ($asset->status) ? [
                'id' => (int) $asset->status->id,
                'name' => e($asset->status->name),
                'status_type' => e($asset->status->getStatuslabelType()),
                'status_meta' => e($asset->present()->statusMeta),
            ] : null,
            'category' => (($asset->model) && ($asset->model->category)) ? [
                'id' => (int) $asset->model->category->id,
                'name' => e($asset->model->category->name),
                'tag_color' => ($asset->model->category->tag_color) ? e($asset->model->category->tag_color) : null,
            ] : null,
            'manufacturer' => (($asset->model) && ($asset->model->manufacturer)) ? [
                'id' => (int) $asset->model->manufacturer->id,
                'name' => e($asset->model->manufacturer->name),
                'tag_color' => ($asset->model->manufacturer->tag_color) ? e($asset->model->manufacturer->tag_color) : null,
            ] : null,
            'depreciation' => (($asset->model) && ($asset->model->depreciation)) ? [
                'id' => (int) $asset->model->depreciation->id,
                'name' => e($asset->model->depreciation->name),
                'months' => (int) $asset->model->depreciation->months,
                'type' => e($asset->model->depreciation->depreciation_type),
                'minimum' => ($asset->model->depreciation->depreciation_min) ? (int) $asset->model->depreciation->depreciation_min : null,
            ] : null,
            'supplier' => ($asset->supplier) ? [
                'id' => (int) $asset->supplier->id,
                'name' => e($asset->supplier->name),
                'tag_color' => ($asset->supplier->tag_color) ? e($asset->supplier->tag_color) : null,
            ] : null,
            'notes' => ($asset->notes) ? Helper::parseEscapedMarkedownInline($asset->notes) : null,
            'order_number' => ($asset->order_number) ? e($asset->order_number) : null,
            'company' => ($asset->company) ? [
                'id' => (int) $asset->company->id,
                'name' => e($asset->company->name),
                'tag_color' => ($asset->company->tag_color) ? e($asset->company->tag_color) : null,
            ] : null,
            'location' => ($asset->location) ? [
                'id' => (int) $asset->location->id,
                'name' => e($asset->location->name),
                'tag_color' => ($asset->location->tag_color) ? e($asset->location->tag_color) : null,
            ] : null,
            'rtd_location' => ($asset->defaultLoc) ? [
                'id' => (int) $asset->defaultLoc->id,
                'name' => e($asset->defaultLoc->name),
                'tag_color' => ($asset->defaultLoc->tag_color) ? e($asset->defaultLoc->tag_color) : null,
            ] : null,
            'image' => ($asset->getImageUrl()) ? $asset->getImageUrl() : null,
            'qr_code_url' => route('qr_code/common', ['object_type' => 'hardware', 'id' => $asset->id]),
            'qr' => ($setting->qr_code == '1') ? Storage::disk('public')->url('barcodes/qr-'.str_slug($asset->asset_tag).'-'.str_slug($asset->id).'.png') : null,
            'alt_barcode' => ($setting->alt_barcode_enabled == '1') ? Storage::disk('public')->url('barcodes/'.str_slug($setting->alt_barcode).'-'.str_slug($asset->asset_tag).'.png') : null,
            'assigned_to' => $this->transformAssignedTo($asset),
            'warranty_months' => ($asset->warranty_months > 0) ? e($asset->warranty_months.' '.trans('admin/hardware/form.months')) : null,
            'warranty_expires' => ($asset->warranty_months > 0) ? Helper::getFormattedDateObject($asset->warranty_expires, 'date') : null,
            'created_by' => ($asset->adminuser) ? [
                'id' => (int) $asset->adminuser->id,
                'name' => e($asset->adminuser->display_name),
            ] : null,
            'created_at' => Helper::getFormattedDateObject($asset->created_at, 'datetime'),
            'updated_at' => Helper::getFormattedDateObject($asset->updated_at, 'datetime'),
            'last_audit_date' => Helper::getFormattedDateObject($asset->last_audit_date, 'datetime'),
            'next_audit_date' => Helper::getFormattedDateObject($asset->next_audit_date, 'date'),
            'deleted_at' => Helper::getFormattedDateObject($asset->deleted_at, 'datetime'),
            'purchase_date' => Helper::getFormattedDateObject($asset->purchase_date, 'date'),
            //            'first_checkout' => Helper::getFormattedDateObject($asset->first_checkout_at, 'datetime'),
            'age' => $asset->purchase_date ? $asset->purchase_date->locale(app()->getLocale())->diffForHumans() : '',
            'last_checkout' => Helper::getFormattedDateObject($asset->last_checkout, 'datetime'),
            'last_checkin' => Helper::getFormattedDateObject($asset->last_checkin, 'datetime'),
            'expected_checkin' => Helper::getFormattedDateObject($asset->expected_checkin, 'datetime'),
            'purchase_cost' => Helper::formatCurrencyOutput($asset->purchase_cost),
            'checkin_counter' => (int) $asset->checkin_counter,
            'checkout_counter' => (int) $asset->checkout_counter,
            'requests_counter' => (int) $asset->requests_counter,
            'user_can_checkout' => (bool) $asset->availableForCheckout(),
            'book_value' => Helper::formatCurrencyOutput($asset->getDepreciatedValue()),
        ];

        if (($asset->model) && ($asset->model->fieldset) && ($asset->model->fieldset->fields->count() > 0)) {
            $fields_array = [];

            foreach ($asset->model->fieldset->fields as $field) {
                if ($field->isFieldDecryptable($asset->{$field->db_column})) {
                    $decrypted = Helper::gracefulDecrypt($field, $asset->{$field->db_column});
                    $value = (Gate::allows('assets.view.encrypted_custom_fields')) ? $decrypted : strtoupper(trans('admin/custom_fields/general.encrypted'));

                    if ($field->format == 'DATE' || $field->format == 'DATETIME') {
                        if (Gate::allows('assets.view.encrypted_custom_fields')) {
                            $value = Helper::getFormattedDateObject($value, $field->format == 'DATETIME' ? 'datetime' : 'date', false);
                        } else {
                            $value = strtoupper(trans('admin/custom_fields/general.encrypted'));
                        }
                    }

                    $fields_array[$field->name] = [
                        'field' => e($field->db_column),
                        'value' => ($field->element == 'markdown-textarea' && Gate::allows('assets.view.encrypted_custom_fields')) ? Helper::renderMarkdown($value) : e($value),
                        'field_format' => $field->format,
                        'element' => $field->element,
                    ];

                } else {
                    $value = $asset->{$field->db_column};

                    if ((in_array($field->format, ['DATE', 'DATETIME'])) && (! is_null($value)) && ($value != '')) {
                        $value = Helper::getFormattedDateObject($value, $field->format == 'DATETIME' ? 'datetime' : 'date', false);
                    }

                    $fields_array[$field->name] = [
                        'field' => e($field->db_column),
                        'value' => ($field->element == 'markdown-textarea') ? Helper::renderMarkdown($value) : e($value),
                        'field_format' => $field->format,
                        'element' => $field->element,
                    ];
                }

                $array['custom_fields'] = $fields_array;
            }
        } else {
            $array['custom_fields'] = new \stdClass; // HACK to force generation of empty object instead of empty list
        }

        $permissions_array['available_actions'] = [
            'checkout' => ($asset->deleted_at == '' && Gate::allows('checkout', Asset::class)) ? true : false,
            'checkin' => ($asset->deleted_at == '' && Gate::allows('checkin', Asset::class)) ? true : false,
            'clone' => Gate::allows('create', Asset::class) ? true : false,
            'restore' => ($asset->deleted_at != '' && Gate::allows('create', Asset::class)) ? true : false,
            'update' => ($asset->deleted_at == '' && Gate::allows('update', Asset::class)) ? true : false,
            'audit' => Gate::allows('audit', Asset::class) ? true : false,
            'delete' => ($asset->deleted_at == '' && $asset->assigned_to == '' && Gate::allows('delete', Asset::class) && ($asset->deleted_at == '')) ? true : false,
            'bulk_selectable' => [
                'edit' => ($asset->deleted_at == '' && Gate::allows('update', Asset::class)),
                'maintenance' => ($asset->deleted_at == '' && Gate::allows('update', Asset::class)),
                'checkout' => ($asset->deleted_at == '' && ! $asset->assigned_to && Gate::allows('checkout', Asset::class)),
                'checkin' => ($asset->deleted_at == '' && $asset->assigned_to && Gate::allows('checkin', Asset::class)),
                'audit' => ($asset->deleted_at == '' && Gate::allows('audit', Asset::class)),
                'delete' => ($asset->deleted_at == '' && ! $asset->assigned_to && Gate::allows('delete', Asset::class)),
                'labels' => $asset->deleted_at == '',
                'restore' => ($asset->deleted_at != '' && Gate::allows('create', Asset::class)),
            ],
        ];

        if (request('components') == 'true') {

            if ($asset->components) {
                $array['components'] = [];

                foreach ($asset->components as $component) {
                    // Info-disclosure guard: if the caller is denied view
                    // on this specific component, omit it from the response
                    // entirely - not even id / pivot_id are exposed, so a
                    // caller with an explicit components.view deny can't
                    // enumerate what's on the asset.
                    if (Gate::denies('view', $component)) {
                        continue;
                    }

                    $unitCost = $component->lastOrderDefaults()['unit_cost'] ?? null;
                    $assignedQty = $component->pivot->assigned_qty;

                    $array['components'][] = [
                        'id' => $component->id,
                        'pivot_id' => $component->pivot->id,
                        'name' => e($component->name),
                        'qty' => $assignedQty,
                        'purchase_cost' => $unitCost,
                        'purchase_total' => ($unitCost !== null && $assignedQty !== null)
                            ? (float) $unitCost * (int) $assignedQty
                            : null,
                        'checkout_date' => Helper::getFormattedDateObject($component->pivot->created_at, 'datetime'),
                    ];
                }
            }

        }

        $array += $permissions_array;

        return $array;
    }

    public function transformAssetsDatatable($assets)
    {
        return (new DatatablesTransformer)->transformDatatables($assets);
    }

    public function transformAssignedTo($asset)
    {
        if (! $asset->assigned) {
            return null;
        }

        if ($asset->checkedOutToUser()) {
            // Info-disclosure guard: this shape is embedded in every
            // asset response (index, show, checkout-listings). A caller
            // with assets.view but not users.view used to read the
            // assignee's username / email / employee number / jobtitle
            // straight out of a hardware payload. Denied fallback keeps
            // id / type / name because someone with assets.view
            // legitimately needs to know WHO has an asset - what gets
            // stripped is PII (username, email, employee_num, jobtitle,
            // first/last name split). Instance check so FMCS scoping
            // applies too.
            if (Gate::denies('view', $asset->assigned)) {
                return [
                    'id' => (int) $asset->assigned->id,
                    'type' => 'user',
                    'name' => e($asset->assigned->display_name),
                ];
            }

            return [
                'id' => (int) $asset->assigned->id,
                'username' => e($asset->assigned->username),
                'name' => e($asset->assigned->display_name),
                'first_name' => e($asset->assigned->first_name),
                'last_name' => ($asset->assigned->last_name) ? e($asset->assigned->last_name) : null,
                'email' => ($asset->assigned->email) ? e($asset->assigned->email) : null,
                'employee_number' => ($asset->assigned->employee_num) ? e($asset->assigned->employee_num) : null,
                'jobtitle' => $asset->assigned->jobtitle ? e($asset->assigned->jobtitle) : null,
                'type' => 'user',
            ];
        }

        // Assets can be checked out to assets or locations. Basic
        // identity (name) is preserved without a permission gate on the
        // target: someone with assets.view legitimately needs to know
        // which location an asset is at or which parent asset it belongs
        // to, and neither is PII.
        return [
            'id' => $asset->assigned->id,
            'name' => e($asset->assigned->display_name),
            'type' => $asset->assignedType(),
        ];
    }

    public function transformRequestedAssets(Collection $assets, $total)
    {
        $array = [];
        foreach ($assets as $asset) {
            $array[] = self::transformRequestedAsset($asset);
        }

        return (new DatatablesTransformer)->transformDatatables($array, $total);
    }

    public function transformRequestedAsset(Asset $asset)
    {
        $array = [
            'id' => (int) $asset->id,
            'name' => e($asset->name),
            'asset_tag' => e($asset->asset_tag),
            'serial' => e($asset->serial),
            'image' => ($asset->getImageUrl()) ? $asset->getImageUrl() : null,
            'model' => ($asset->model) ? e($asset->model->name) : null,
            'model_number' => (($asset->model) && ($asset->model->model_number)) ? e($asset->model->model_number) : null,
            'expected_checkin' => Helper::getFormattedDateObject($asset->expected_checkin, 'datetime'),
            'location' => ($asset->location) ? e($asset->location->name) : null,
            'status' => ($asset->status) ? $asset->present()->statusMeta : null,
            'assigned_to_self' => ($asset->assigned_to == auth()->id()),
        ];

        if (($asset->model) && ($asset->model->fieldset) && ($asset->model->fieldset->fields->count() > 0)) {
            $fields_array = [];

            foreach ($asset->model->fieldset->fields as $field) {

                // Only display this if it's allowed via the custom field setting
                if (($field->field_encrypted == '0') && ($field->show_in_requestable_list == '1')) {

                    $value = $asset->{$field->db_column};
                    if ((in_array($field->format, ['DATE', 'DATETIME'])) && (! is_null($value)) && ($value != '')) {
                        $value = Helper::getFormattedDateObject($value, $field->format == 'DATETIME' ? 'datetime' : 'date', false);
                    }

                    $fields_array[$field->db_column] = ($field->element == 'markdown-textarea') ? Helper::renderMarkdown($value) : e($value);
                }

                $array['custom_fields'] = $fields_array;
            }
        } else {
            $array['custom_fields'] = new \stdClass; // HACK to force generation of empty object instead of empty list
        }

        $permissions_array['available_actions'] = [
            'cancel' => ($asset->isRequestedBy(auth()->user())) ? true : false,
            'request' => ($asset->isRequestedBy(auth()->user())) ? false : true,
        ];

        $array += $permissions_array;

        return $array;
    }

    public function transformAssetCompact(Asset $asset)
    {
        $array = [
            'id' => (int) $asset->id,
            'image' => ($asset->getImageUrl()) ? $asset->getImageUrl() : null,
            'type' => 'asset',
            'name' => e($asset->display_name),
            'model' => ($asset->model) ? e($asset->model->name) : null,
            'model_number' => (($asset->model) && ($asset->model->model_number)) ? e($asset->model->model_number) : null,
            'asset_tag' => e($asset->asset_tag),
            'serial' => e($asset->serial),
        ];

        return $array;
    }

    public function transformCheckedoutAccessories($accessory_checkouts, $total)
    {

        $array = [];
        foreach ($accessory_checkouts as $checkout) {
            $array[] = self::transformCheckedoutAccessory($checkout);
        }

        return (new DatatablesTransformer)->transformDatatables($array, $total);
    }

    public function transformCheckedoutAccessory(AccessoryCheckout $accessory_checkout)
    {
        if ($accessory_checkout->accessory) {
            $array = [
                'id' => $accessory_checkout->id,
                'accessory' => [
                    'id' => $accessory_checkout->accessory->id,
                    'name' => e($accessory_checkout->accessory->display_name),
                ],
                'assigned_to' => $accessory_checkout->assigned_to,
                'image' => ($accessory_checkout->accessory->image) ? Storage::disk('public')->url('accessories/'.e($accessory_checkout->accessory->image)) : null,
                'note' => $accessory_checkout->note ? e($accessory_checkout->note) : null,
                'created_by' => $accessory_checkout->adminuser ? [
                    'id' => (int) $accessory_checkout->adminuser->id,
                    'name' => e($accessory_checkout->display_name),
                ] : null,
                'created_at' => Helper::getFormattedDateObject($accessory_checkout->created_at, 'datetime'),
                'deleted_at' => Helper::getFormattedDateObject($accessory_checkout->deleted_at, 'datetime'),
            ];

            $permissions_array['available_actions'] = [
                'checkout' => false,
                'checkin' => Gate::allows('checkin', Accessory::class),
            ];

            $array += $permissions_array;

            return $array;
        }
    }

    public function transformLicensesCheckedToAsset($license_checkouts, $total)
    {

        $array = [];
        foreach ($license_checkouts as $checkout) {
            $array[] = self::transformLicenseCheckedToAsset($checkout);
        }

        return (new DatatablesTransformer)->transformDatatables($array, $total);
    }

    public function transformLicenseCheckedToAsset(LicenseSeat $licenseseat)
    {

        if (Gate::allows('viewKeys', $licenseseat->license)) {
            $product_key = $licenseseat->license->serial ?? null;
        } else {
            $product_key = License::PRODUCT_KEY_MASK;
        }

        $array = [
            'id' => $licenseseat->id,
            'license' => [
                'id' => $licenseseat->license?->id,
                'name' => e($licenseseat->license?->display_name),
                'serial' => $product_key ? e($product_key) : null,
                'note' => $licenseseat->license?->note ? e($licenseseat->license?->note) : null,

            ],
            'assigned_asset' => $licenseseat->asset_id,
            'expiration_date' => $licenseseat->license?->expiration_date ? Helper::getFormattedDateObject($licenseseat->license?->expiration_date, 'date') : null,
            'notes' => $licenseseat->notes ? e($licenseseat->notes) : null,
        ];

        $permissions_array['available_actions'] = [
            'checkout' => false,
            'checkin' => Gate::allows('checkin', License::class),
            'bulk_selectable' => [
                'checkin' => Gate::allows('checkin', License::class),
            ],
        ];

        $array += $permissions_array;

        return $array;

    }

    public function transformCheckedoutComponents(Collection $components_assets, $total)
    {
        $array = [];
        $suppressed = 0;
        foreach ($components_assets as $component_checkout) {
            $component = $component_checkout->component;

            // Info-disclosure guard: GET /api/v1/hardware/{asset}/assigned/components
            // is gated only on assets.view, so a caller with assets.view but
            // an explicit deny on components.view used to read the component's
            // name / qty / note straight off this response. When denied, skip
            // the row entirely so nothing about the component (not even id
            // or existence) is exposed. Instance check so FMCS scoping
            // applies too.
            //
            // The controller-supplied $total still reflects the true row
            // count and would leak "there are N components you can't see",
            // so decrement it by the number of rows suppressed here.
            if (! $component || Gate::denies('view', $component)) {
                $suppressed++;

                continue;
            }

            $array[] = [
                'assigned_pivot_id' => $component_checkout->id,
                'name' => [
                    'id' => $component->id,
                    'name' => e($component->display_name),
                    'type' => 'component',
                    'deleted_at' => $component->deleted_at,
                ],
                'assigned_qty' => $component_checkout->assigned_qty,
                'note' => ($component_checkout->note) ? e($component_checkout->note) : null,
                'created_at' => Helper::getFormattedDateObject($component_checkout->created_at, 'datetime'),
                'created_by' => $component_checkout->adminuser ? [
                    'id' => (int) $component_checkout->adminuser->id,
                    'name' => e($component_checkout->adminuser->display_name),
                ] : null,
                'available_actions' => [
                    'checkin' => (($component->deleted_at == '') && Gate::allows('checkin', Component::class)),
                    'view' => (($component->deleted_at == '') && Gate::allows('view', Component::class)),
                ],
            ];
        }

        return (new DatatablesTransformer)->transformDatatables($array, max(0, $total - $suppressed));
    }
}
