#!/usr/bin/perl

use strict;

BEGIN {
  $|  = 1;
  $^W = 1;
}

use lib "t/lib";
use SQLeetTest qw/connect_ok has_sqleet_sqlite/;
use Test::More;
use DBD::SQLeet;

my @texts = ("il �tait une berg�re",
             "qui gardait ses moutons",
             "elle fit un fromage",
             "du lait de ses moutons");

my @tests = (
#  query                  => expected results
  ["berg�re"              => 0       ],
  ["berg*"                => 0       ],
  ["foobar"                          ],
  ["moutons"              => 1, 3    ],
  ['"qui gardait"'        => 1       ],
  ["moutons NOT lait"     => 1       ],
  ["il �tait"             => 0       ],
  ["(il OR elle) AND un*" => 0, 2    ],
);

BEGIN {
  if ($] < 5.008005) {
    plan skip_all => 'Unicode is not supported before 5.8.5';
  }
  if (!grep /ENABLE_FTS3/, DBD::SQLeet::compile_options()) {
    plan skip_all => 'FTS3 is disabled for this DBD::SQLeet';
  }
  if ($DBD::SQLeet::sqlite_version_number >= 3011000 and $DBD::SQLeet::sqlite_version_number < 3012000 and !grep /ENABLE_FTS3_TOKENIZER/, DBD::SQLeet::compile_options()) {
    plan skip_all => 'FTS3 tokenizer is disabled for this DBD::SQLeet';
  }
}

# Perl may spit a warning on locale
# use Test::NoWarnings;

my $num = has_sqleet_sqlite('3.7.4') ? 4 : 2;

plan tests => $num * @tests # each test with unicode y/n and with fts3/fts4
            + 2;           # connect_ok with unicode y/n

BEGIN {
  # Sadly perl for windows (and probably sqlite, too) may hang
  # if the system locale doesn't support european languages.
  # en-us should be a safe default. if it doesn't work, use 'C'.
  if ( $^O eq 'MSWin32') {
    use POSIX 'locale_h';
    setlocale(LC_COLLATE, 'en-us');
  }
}
use locale;


sub locale_tokenizer { # see also: Search::Tokenizer
  return sub {
    my $string = shift;

    my $regex      = qr/\w+/;
    my $term_index = 0;

    return sub {
      $string =~ /$regex/g or return; # either match, or no more token
      my ($start, $end) = ($-[0], $+[0]);
      my $term = substr($string, $start, my $len = $end-$start);
      return ($term, $len, $start, $end, $term_index++);
    };
  };
}

use DBD::SQLeet;

for my $use_unicode (0, 1) {

  # connect
  my $dbh = connect_ok( RaiseError => 1, sqlite_unicode => $use_unicode );

  for my $fts (qw/fts3 fts4/) {
    next if $fts eq 'fts4' && !has_sqleet_sqlite('3.7.4');
    # create fts table
    $dbh->do(<<"") or die DBI::errstr;
      CREATE VIRTUAL TABLE try_$fts
        USING $fts(content, tokenize=perl 'main::locale_tokenizer')

    # populate it
    my $insert_sth = $dbh->prepare(<<"") or die DBI::errstr;
      INSERT INTO try_$fts(content) VALUES(?)

    my @doc_ids;
    for (my $i = 0; $i < @texts; $i++) {
      $insert_sth->execute($texts[$i]);
      $doc_ids[$i] = $dbh->last_insert_id("", "", "", "");
    }

    # queries
    SKIP: {
      skip "These tests require SQLite compiled with "
         . "ENABLE_FTS3_PARENTHESIS option", scalar @tests
        unless DBD::SQLeet->can('compile_options') &&
        grep /ENABLE_FTS3_PARENTHESIS/, DBD::SQLeet::compile_options();
      my $sql = "SELECT docid FROM try_$fts WHERE content MATCH ?";
      for my $t (@tests) {
        my ($query, @expected) = @$t;
        @expected = map {$doc_ids[$_]} @expected;
        my $results = $dbh->selectcol_arrayref($sql, undef, $query);
        is_deeply($results, \@expected, "$query ($fts, unicode=$use_unicode)");
      }
    }
  }
}
