<?php

namespace App\Repositories\Shop;

use App\Datatables\Shop\CustomerOrdersDataTable;
use App\Models\Shop\Customer;
use App\Repositories\Core\File;
use App\Traits\Model\Basic;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;
use Laravolt\Avatar\Avatar;

class Customers
{
    use Basic;

    public static function getOptions()
    {
        return Customer::pluck('last_name', 'id');
    }

    public static function editProfile($id = false)
    {
        $id = $id ? $id : self::getId();
        $datatableOrders = new CustomerOrdersDataTable();
        $data = [
            'customer' => self::get($id, ['addresses', 'deliveries'])->toArray(),
            'deliveries' => Deliveries::getAllWithSaleChannel()->toArray(),
            'orders' => $datatableOrders->html(),
        ];

        return $data;
    }

    public static function getAvatar($id = false)
    {
        $customer = $id ? self::get($id) : self::getAuth();
        $file = self::makeAvatarFilename($customer);
        if (! File::checkFile($file)) {
            self::createAvatar($customer);
        }

        return self::getPublic(self::getAvatarFilename($customer));
    }

    public static function createAvatar($customer)
    {
        $filename = self::makeAvatarFilename($customer);
        $name = $customer->first_name.' '.$customer->last_name;
        $avatar = new Avatar();
        $ret = $avatar->create($name)
            ->setBackground('#F2B90F')
            ->setForeground('#335012')
            ->setBorder(1, '#28a745')
            ->setFontFamily('Roboto Condensed')
            ->setDimension(36)
            ->setFontSize(16)
            ->save($filename);

        return $ret;
    }

    public static function makeAvatarFilename($customer)
    {
        $path = storage_path(self::getStorage());
        if (File::checkDirOrCreate($path)) {
            $filename = $path.self::getAvatarFilename($customer);
        }

        return $filename ?? false;
    }

    public static function getAvatarFilename($customer)
    {
        return 'user-'.$customer->uuid.'.png';
    }

    public static function getAddresses($id = false)
    {
        $customer = self::getWithAddresses($id);

        return $customer ? $customer->addresses : false;
    }

    public static function getWithAddresses($id = false)
    {
        $id = $id ? $id : self::getId();

        return self::get($id, ['invoice_addresses', 'delivery_addresses']);
    }

    public static function getName($id = false)
    {
        $user = $id ? self::get($id) : self::getAuth();

        return $user ? $user->first_name.' '.$user->last_name : '';
    }

    public static function getAuth()
    {
        return self::guard()->user();
    }

    public static function getId()
    {
        return self::guard()->id();
    }

    public static function isNotConnected()
    {
        return ! self::isConnected();
    }

    public static function isConnected()
    {
        return self::guard()->check();
    }

    public static function edit($id)
    {
        $customer = self::get($id, ['delivery_addresses', 'invoice_addresses']);
        $data = $customer->toArray();
        $data['deliveries'] = $customer->deliveries->pluck('id')->toArray();

        return $data;
    }

    public static function storeFull($data)
    {
        $deliveries = $data['deliveries'];
        $invoices = $data['invoices'];
        unset($data['deliveries']);
        unset($data['invoices']);
        $customer = self::store($data);
        self::storeAddresses($customer, $deliveries);
        self::storeAddresses($customer, $invoices);

        return $customer->id;
    }

    public static function storeDeliveries($customer, $deliveries)
    {
        if (! $deliveries) {
            return false;
        }
        $deliveries = collect($deliveries)->transform(
            function ($item) {
                return (int) $item;
            }
        )->toArray();

        return $customer->deliveries()->sync($deliveries);
    }

    public static function storeAddresses($customer, $addresses)
    {
        foreach ($addresses as $address) {
            $address['customer_id'] = $customer->id;
            CustomerAddresses::store($address);
        }
    }

    public static function create($data)
    {
        $user = Customer::create([
            'uuid' => Str::uuid(),
            'active' => true,
            'first_name' => $data['first_name'],
            'last_name' => $data['last_name'],
            'company' => $data['company'],
            'tva' => $data['tva'],
            'phone' => $data['phone'],
            'address' => $data['address'],
            'address2' => $data['address2'],
            'zipcode' => $data['zipcode'],
            'city' => $data['city'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
        ]);

        return $user;
    }

    public static function getStorage($filename = false)
    {
        $path = '/app/public/Customers/';

        return $filename ? $path.$filename : $path;
    }

    public static function getPublic($filename = false)
    {
        $path = '/storage/Customers/';

        return $filename ? $path.$filename : $path;
    }

    public static function guard()
    {
        return Auth::guard('customer');
    }

    public static function getModel()
    {
        return Customer::query();
    }
}
