<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use Carbon\Carbon;

class BackendPermissionsTableSeeder extends Seeder
{
    private $table = 'backend_permissions';

    protected function _getData()
    {
        $now = Carbon::now();

        return [
            [
                'id'           => 1,
                'group_id'     => 1,
                'name'         => 'users.view',
                'display_name' => 'View backoffice users and permissions',
                'position'     => 1,
                'created_at'   => $now,
            ],
            [
                'id'           => 2,
                'group_id'     => 1,
                'name'         => 'users.insert',
                'display_name' => 'Add backoffice users and define their permissions',
                'position'     => 2,
                'created_at'   => $now,
            ],
            [
                'id'           => 3,
                'group_id'     => 2,
                'name'         => 'cms.manage',
                'display_name' => 'Content management',
                'position'     => 1,
                'created_at'   => $now,
            ],
            [
                'id'           => 4,
                'group_id'     => 3,
                'name'         => 'accounts.manage',
                'display_name' => 'Accounts management',
                'position'     => 1,
                'created_at'   => $now,
            ],
        ];
    }

    /**
     * Run the database seeders.
     *
     * @return void
     */
    public function run()
    {
        $data = $this->_getData();

        foreach ($data as $dataItem) {
            $dbItem = DB::table($this->table)->where([
                ['id', '=', $dataItem['id']],
                ['group_id', '=', $dataItem['group_id']],
            ])->first();

            if (!empty($dbItem)) {
                continue;
            }

            DB::table($this->table)->insert($dataItem);
        }
    }
}
