<?php
namespace factory;

class Wiki
{
  static public function get_all_categories()
  {
    global $mdb;
    return $mdb -> select( 'wiki', '*', [ 'ORDER' => [ 'name' => 'ASC' ] ] );
  }

  static public function category_delete( int $category_id ) {
    global $mdb;

    $category_id = (int)$category_id;
    if ( !$category_id )
      return false;

    $mdb -> delete( 'wiki_users', [ 'wiki_id' => $category_id ] );
    $mdb -> delete( 'task_wiki', [ 'wiki_id' => $category_id ] );
    return $mdb -> delete( 'wiki', [ 'id' => $category_id ] );
  }

  static public function categories_delete_bulk( array $category_ids )
  {
    global $mdb;

    $ids = [];
    foreach ( $category_ids as $category_id )
    {
      $category_id = (int)$category_id;
      if ( $category_id > 0 )
        $ids[] = $category_id;
    }

    $ids = array_values( array_unique( $ids ) );
    if ( !count( $ids ) )
      return 0;

    $mdb -> delete( 'wiki_users', [ 'wiki_id' => $ids ] );
    $mdb -> delete( 'task_wiki', [ 'wiki_id' => $ids ] );
    $mdb -> delete( 'wiki', [ 'id' => $ids ] );

    return count( $ids );
  }

  public static function category_save( $category_id, $name, $text, $text_admin = '', $users )
  {
    global $mdb;

    if ( !$category_id )
    {
      $mdb -> insert( 'wiki', [
        'name' => $name,
        'text' => $text,
        'text_admin' => $text_admin ? $text_admin : null
      ] );
      $category_id = $mdb -> id();
    }
    else
    {
      $mdb -> update( 'wiki', [
        'text' => $text,
        'name' => $name,
        'text_admin' => $text_admin ? $text_admin : null
      ], [
        'id' => $category_id
      ] );
      $mdb -> delete( 'wiki_users', [ 'wiki_id' => $category_id ] );
    }

    if ( is_array( $users ) ) foreach ( $users as $key => $val ) {
      $mdb -> insert( 'wiki_users', [
        'wiki_id' => $category_id,
        'user_id' => $val
      ] );
    }
    return $category_id;
  }

  public static function category_details( $category_id ) {
    global $mdb;
    $category = $mdb -> get('wiki', '*', [ 'id' => $category_id ]);
    $category['users'] = $mdb -> select( 'wiki_users', 'user_id', [ 'wiki_id' => $category_id ] );
    return $category;
  }

  static public function category_users( $category_id )
  {
    global $mdb;
    return $mdb -> select( 'wiki_users', 'user_id', [ 'wiki_id' => $category_id ] );
  }

  static public function get_categories()
  {
    global $mdb, $user;

    return self::get_categories_for_user( (int)$user['id'] );
  }

  static public function get_categories_for_user( int $user_id )
  {
    global $mdb;

    if ( $user_id == 1 or $user_id == 3 )
      return $mdb -> select( 'wiki', '*', [ 'ORDER' => [ 'name' => 'ASC' ] ] );

    return $mdb -> query(
      'SELECT DISTINCT w.* FROM wiki AS w '
      . 'LEFT JOIN wiki_users AS wu ON wu.wiki_id = w.id '
      . 'WHERE wu.user_id = ' . $user_id . ' OR NOT EXISTS (SELECT 1 FROM wiki_users wu2 WHERE wu2.wiki_id = w.id) '
      . 'ORDER BY w.name ASC'
    ) -> fetchAll( \PDO::FETCH_ASSOC );
  }

  static public function is_category_visible_for_user( int $category_id, int $user_id ): bool
  {
    global $mdb;

    if ( !$category_id or !$user_id )
      return false;

    if ( $user_id == 1 or $user_id == 3 )
      return (bool)$mdb -> count( 'wiki', [ 'id' => $category_id ] );

    $visibility_rows = (int)$mdb -> count( 'wiki_users', [ 'wiki_id' => $category_id ] );
    if ( $visibility_rows === 0 )
      return (bool)$mdb -> count( 'wiki', [ 'id' => $category_id ] );

    return (bool)$mdb -> count( 'wiki_users', [ 'AND' => [ 'wiki_id' => $category_id, 'user_id' => $user_id ] ] );
  }
}
