use 5.10.1;
use strict;
use warnings;

# ABSTRACT: Example application
# AUTHORITY
# PODNAME: dbic-schema-viewer
our $VERSION = '0.0200';

use Mojolicious::Lite;
use File::HomeDir;
use Path::Tiny;
use syntax 'qi';
use String::Stomp;
use String::Random;

my $confdir = path(File::HomeDir->my_dist_data('Mojolicious-Plugin-DbicSchemaViewer', { create => 1 }));
my $logdir = $confdir->child('log');
$logdir->mkpath;
app->log->path($logdir->child('dbic-schema-viewer.log')->stringify);

my $exists = $confdir->exists && $confdir->is_dir ? 1 : 0;

app->log->debug("[dbic-schema-viewer] Looking for config in $confdir/dbic-schema-viewer.conf ...");

my $confile = $confdir->child('dbic-schema-viewer.conf');
app->log->path('log');

# setup config file if it doesn't exist
if(!$confile->exists) {
    app->log->debug('[dbic-schema-viewer] Setting up config file at ' . $confile->stringify);
    my $secret = String::Random->new->randregex('[a-z0-9_]{20,20}');
    $confile->spew(stomp qi!
        {
            dbic_schema_viewer => {
                # allowed_schemas => [qw//],
                # router => ...,
                # condition => ...
                # url => 'dbic-schema-viewer',
            },
            secrets => [qw/! . $secret . qi!/],
        };!);
}
plugin 'Config', { file => $confdir->child('dbic-schema-viewer.conf')->stringify };

if(exists app->config->{'secrets'}) {
    app->secrets(app->config->{'secrets'});
}

app->config->{'dbic-schema-viewer'}{'url'} = '/' if !exists app->config->{'dbic-schema-viewer'}{'url'};
plugin 'DbicSchemaViewer', app->config->{'dbic_schema_viewer'};

app->start;

__END__

=pod

=encoding utf-8

=head1 NAME

dbic-schema-viewer - Example application

=head1 VERSION

Version 0.0200, released 2016-09-20.

=head1 SYNOPSIS

    hypnotoad dbic-schema-viewer

=head1 DESCRIPTION

    This is a small application that uses the L<Mojolicious::Plugin::DbicSchemaViewer>. A configuration file will be generated
    at first run.

    It is this application that runs the L<demo|http://dsv.code301.com/MadeUp::Book::Schema>.

=head1 SOURCE

L<https://github.com/Csson/p5-Mojolicious-Plugin-DbicSchemaViewer>

=head1 HOMEPAGE

L<https://metacpan.org/release/Mojolicious-Plugin-DbicSchemaViewer>

=head1 AUTHOR

Erik Carlsson <info@code301.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2016 by Erik Carlsson.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut