use 5.010;
use strict;
use MooseX::Declare;

BEGIN
{
	$WWW::DataWiki::Controller::Container::AUTHORITY = 'cpan:TOBYINK';
	$WWW::DataWiki::Controller::Container::VERSION   = '0.000_01';
}

class WWW::DataWiki::Controller::Container
	extends WWW::DataWiki::Controller
{
	use CatalystX::Syntax::Action;
	use File::Slurp qw(slurp);
	use URI::Escape qw(uri_escape);
	
	our $RE_page = '((?:[a-z][a-z0-9-]*[a-z0-9])(?:/[a-z][a-z0-9-]*[a-z0-9])*)';
	our $RE_path = "${RE_page}/";
	
	__PACKAGE__->config(
		namespace => '',
		action => {
			container   => { Regex => "^${RE_path}\$" },
			end         => { Private => 1 },
			},
		);

	action container { $self->_dispatch_by_method($ctx, 'container'); }

	action container_GET
	{
		my ($wikiname) = @{ $ctx->req->captures || [''] };
		
		my $c = $ctx->model('Wiki')->container("${wikiname}/");
		WWW::DataWiki::Exception->throw(404 => 'Container not found') unless defined $c;
		#WWW::DataWiki::Exception->throw(404 => 'Container not found') unless $c->all_member_names; # somewhat of a hack
		$ctx->stash(resource => $c);

		if (WWW::DataWiki::Controller::Query->req_is_query($ctx->req))
		{
			$ctx->detach('Controller::Query', 'query');
		}		
	}

	action container_POST 
	{
		my ($wikiname) = @{ $ctx->req->captures || [''] };
		
		my $c = $ctx->model('Wiki')->container("${wikiname}/");
		WWW::DataWiki::Exception->throw(404 => 'Container not found') unless defined $c;
		#WWW::DataWiki::Exception->throw(404 => 'Container not found') unless $c->all_member_names; # somewhat of a hack
		$ctx->stash(resource => $c);
		
		if (WWW::DataWiki::Controller::Query->req_is_query($ctx->req))
		{
			$ctx->detach('Controller::Query', 'query');
		}
		elsif (defined (my $fmt = WWW::DataWiki::Utils->canonicalise_rdf_format($ctx->req->content_type)))
		{
			WWW::DataWiki::Exception->throw(500 => 'Not implemented',
				'Should create a new resource in the collection with a random URI.');
		}
		else
		{
			WWW::DataWiki::Exception->throw(415 => 'Unsupported media type',
				'This resource accepts POST requests using SPARQL Query, and accepts RDF data to create a new resource in the collection.');
		}
	}

	action container_HEAD { goto &container_GET; }
}

1;
