<?php

use Carbon\Carbon;
use AuthStack\Models\Mapping;
use Illuminate\Database\Seeder;
use AuthStack\Models\DriverConnection;
use AuthStack\Models\Driver as DriverModel;

class AUDGenericDriverSeeder extends Seeder
{
    public function run()
    {
        $now = Carbon::now('UTC')->format('Y-m-d H:i:s');

        $driver = [
            'unique_id' => sha1('AuthStack\\Driver\\AUDGeneric\\Driver', true),
            'is_protected' => 0,
            'title' => 'AUD Generic Driver',
            'description' => 'AUD generic driver connection',
            'class_path' => 'AuthStack\\Driver\\AUDGeneric\\Driver',
            'config' => [
                'schema' => [
                    '$schema' => 'http://json-schema.org/draft-04/schema#',
                    'properties' => [
                        'endpoint' => [
                            'type' => 'object',
                            'required' => [
                                'url',
                                'method',
                                'path',
                                'verify',
                                'description',
                            ],
                            'properties' => [
                                'url' => [
                                    'type' => 'string',
                                ],
                                'path' => [
                                    'type' => 'string',
                                ],
                                'method' => [
                                    'type' => 'string',
                                ],
                                'verify' => [
                                    'type' => 'boolean',
                                ],
                                'description' => [
                                    'type' => 'string',
                                ],
                            ],
                        ],
                        'attributes' => [
                            'type' => 'array',
                            'items' => [
                                'type' => 'object',
                                'required' => [
                                    'id',
                                    'source_id',
                                    'email',
                                    'username_readable',
                                    'first_name',
                                    'last_name',
                                ],
                                'properties' => [
                                    'id' => [
                                        'type' => ['string', 'number'],
                                    ],
                                    'source_id' => [
                                        'type' => ['string', 'number'],
                                    ],
                                    'email' => [
                                        'type' => 'string',
                                    ],
                                    'username_readable' => [
                                        'type' => 'string',
                                    ],
                                    'first_name' => [
                                        'type' => 'string',
                                    ],
                                    'last_name' => [
                                        'type' => 'string',
                                    ],
                                ],
                            ],
                        ],
                    ],
                ],
            ],
            'num_connections' => '0',
            'created_at' => $now
        ];

        $audURL = env('AUD_URL', false);
        $audVerify = env('AUD_VERIFY_SSL', true);

        if ($audURL) {
            $model = DriverModel::create($driver);

            $conn = DriverConnection::create([
                'driver_id' => $model->id,
                'title' => 'AUD Generic Authentication Connection',
                'is_protected' => 0,
                'configuration' => [
                    "attributes" => [
                        [
                            "id" => 0,
                            "email" => "demo@aud.portal",
                            "last_name" => "Admin",
                            "source_id" => $model->id,
                            "first_name" => "AUD",
                            "username_readable" => "admin",
                        ],
                    ],
                    "get_account" => [
                        "url" => $audURL,
                        "path" => "/api/v1/account/%s",
                        "method" => "GET",
                        "verify" => $audVerify,
                        "description" => "Get account details",
                    ],
                    "get_account_by_username" => [
                        "url" => $audURL,
                        "path" => "/api/v1/account/username/%s",
                        "method" => "GET",
                        "verify" => $audVerify,
                        "description" => "Get account details by username",
                    ],
                    "authenticate" => [
                        "url" => $audURL,
                        "path" => "/api/v1/account/authenticate",
                        "method" => "POST",
                        "verify" => $audVerify,
                        "description" => "Authenticate an account",
                    ],
                ],
                'created_at' => $now
            ]);

            $mapping = Mapping::create([
                'conn_id' => $conn->id,
                'title' => 'AUD Generic Attribute Mapping',
                'description' => 'AUD Generic Attribute Mapping',
                'mappings' => [
                    'attributes' => [
                        [
                            'name' => 'email',
                            'value' => '{{email}}',
                            'isNameID' => true
                        ],

                        [
                            'name' => 'username',
                            'value' => '{{username_readable}}'
                        ],

                        [
                            'name' => 'first_name',
                            'value' => '{{first_name}}'
                        ],

                        [
                            'name' => 'last_name',
                            'value' => '{{last_name}}'
                        ]
                    ],

                    'nameIdFormat' => LightSaml\SamlConstants::NAME_ID_FORMAT_EMAIL,
                    'nameIdAttribute' => 'email',
                    'isTransientNameId' => false
                ]
            ]);
        } else {
            throw new Exception('Missing AUD url on env file');
        }
    }
}
